Autowiring in Spring

Through the autowiring feature, spring will intelligently guess some of the bean injection without needing to configure explicitly. Spring can do autowiring based on the type of the bean, name of the bean, and constructor of the bean. Default is autowire by type.

You can simply specify the @Autowired annotation over properties, setter methods and constructors. Spring will then find a bean of matching type and inject it. By default, it does autowiring by type and hence if you have two matching beans (two beans of same type), your execution will fail.

I am not adding a specific example for @Autowired as you will find its use in many of the examples that follow.



@Qualifier annotation can be used to do autowiring by name. You can specify the name of the bean as @Qualifier("beanName"). 

If you are using Java configuration with @Bean annotations, you can specify the name in your @Bean annotation as @Bean(name="beanName")



To enable @Autowired in older Spring versions that use XML configuration, you have to register ‘AutowiredAnnotationBeanPostProcessor‘ in your Spring xml file:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

Instead you can also do the below to enable all bean post processors at once:

<context:annotation-config />



  1. Try @Autowired annotation over properties, setter methods and constructor.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!