Rahul Agrawal
Rahul Agrawal

Reputation: 8971

What is difference between these 2 Spring IOC injections?

What is difference between these 2 injections ?

@Autowired
private DocumentDAO documentDao;

@Resource(name = "documentDao")
private DocumentDAO documentDao;

Upvotes: 6

Views: 1505

Answers (2)

leewin12
leewin12

Reputation: 1446

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

Upvotes: 10

Rohan
Rohan

Reputation: 266

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.

Upvotes: 9

Related Questions