Spring- Autowire using Annotations

Spring – Autowire using Annotations

Starting with Spring 2.5, the framework introduced a new style of Dependency Injection driven by @Autowired Annotations. This annotation allows Spring to resolve and inject collaborating beans into your bean.

To enable Annotation based autowiring we need to place below line in SpringConfig.xml

<context:annotation-config/>

Once annotation injection is enabled, autowiring can be used on properties, setters, and constructors.


1. @Autowired on Properties

public class Student {	 
	private int sno;
	private String name;
	
	@Autowired
	private Address address;

}

2.@Autowired on Setters

public class Student {
	private int sno;
	private String name;

	private Address address;

	@Autowired
	public void setAddress(Address address) {
 this.address = address;
	}
}

3. @Autowired on Constructors

public class Student {
	private int sno;
	private String name;

	private Address address;
	
	@Autowired
	public Student(Address address) { 
 this.address = address;
	}	 
}

4.@Autowired and Optional Dependencies Spring expects @Autowired dependencies to be available when the dependent bean is being constructed. If the framework cannot resolve a bean for wiring, it will throw NoSuchBeanDefinitionException. To avoid this we have to use (required=false)

public class Student {	 
	private int sno;
	private String name;
	
	@Autowired(required = false)
	private Address address;
}

By default, the @Autowired annotation implies the dependency is required similar to @Required annotation, however, you can turn off the default behavior by using (required=false) option with @Autowired.

5.Autowiring by @Qualifier
By default, Spring resolves @Autowired entries by type. If more than one beans of the same type are available in the container, the framework will throw a fatal exception indicating that more than one bean is available for autowiring.

To avoid this error we have to use @Qualifier.

Now we can update SpringConfig.xml by removing autowire="byType" , because we are using @Autowired annotation

<beans>

 <bean id="student" class="core.Student">
 	<property name="sno" value="101"></property>
 	<property name="name" value="Satya Kaveti"></property>
 </bean>

 <bean id="address" class="core.Address">
 	<property name="hno" value="322"></property>
 	<property name="city" value="HYDERABAD"></property>
 </bean>


 <bean id="address1" class="core.Address">
 	<property name="hno" value="322"></property>
 	<property name="city" value="HYDERABAD"></property>
 </bean>
</beans>

In above you have multiple bean of same type, Container will confuse which bean should inject & throws NoSuchBeanDefinitionException: by: *org.springframework.beans.factory.NoSuchBeanDefinitionException*: No unique bean of type [core.Address] is defined: expected single matching bean but found 2: [address, address1]

To fix above problem, you need @Qualifier to tell Spring about which bean should autowired.

public class Student {
	private int sno;
	private String name;
	@Qualifier("address")
	private Address address;
}