Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between these 2 Spring IOC injections?

Tags:

java

spring

What is difference between these 2 injections ?

@Autowired
private DocumentDAO documentDao;

@Resource(name = "documentDao")
private DocumentDAO documentDao;
like image 324
Rahul Agrawal Avatar asked Jul 12 '12 11:07

Rahul Agrawal


2 Answers

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

But, @Autowired with @Qualifier also can autowire by name as @Resource.

Please take a look below links:

@Autowire

@Resource

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

like image 88
dgregory Avatar answered Sep 28 '22 07:09

dgregory


By default @Autowire inject dependency "by Type". But It can also Inject dependency "by Name" using @Qualifier in conjunction with @Autowire annotation.

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

like image 44
Rohan Avatar answered Sep 28 '22 09:09

Rohan