What is the difference between these 2 Spring IOC injections?

What is the difference between these two injections?

@Autowired private DocumentDAO documentDao; @Resource(name = "documentDao") private DocumentDAO documentDao; 
+6
source share
2 answers

Simply, @Autowired (specification in Spring) wires by type and @Resource (specification in JSR-250) wires by name.

But @Autowired with @Qualifier can also autowire named like @Resource.

Please see below links:

@Autowire

@Resource

@ Spring Injection with @Resource, @Autowired and @Inject

+10
source

By default, @Autowire enter a dependency of type. But it can also introduce dependencies "by name" using @Qualifier in combination with the @Autowire annotation.

But the main difference is that @Autowired is a spring annotation, while @Resource is set by JSR-250. So @Resource is part of normal Java on the other side, @Autowired is only available in Spring.

+9
source

Source: https://habr.com/ru/post/920317/


All Articles