Autowiring spring bean by name using annotation

Anand picture Anand · Aug 6, 2012 · Viewed 75.4k times · Source

In Springs latest version, we can autowire a bean using annotation as @Autowired. This will autowire the bean using its type(or constructor, if applied on it). Is there any way I can use the @Autowired annotation based on the bean name which we were doing without annotation in Spring's XML file as autowire="byName"?

Answer

Biju Kunjummen picture Biju Kunjummen · Aug 6, 2012

You can use:

@Autowired
@Qualifier("beanname")

According to the @Qualifier javadoc

This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring