Spring(9)-Spring Auto-Wiring Bens with@Autowired annotation


In Spring、you can use@Autowired annotation to aut wire bean on the setter method、construtor or a field.Moreover、it can autwired property in a particular bean.
Note
The @Autowired annotation is auto wire the bean by matching data type.
See follwing full example to demostrate the use of@Autowired
1.ビーズ
A customer bean,and declead in bean configration file.Later,you will use'@Autowired'to aut wire a person bean
package com.mkyong.common;
 
public class Customer 
{
	//you want autowired this field.
	private Person person;
 
	private int type;
	private String action;
 
	//getter and setter method
 
}
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address 123" />
		<property name="age" value="28" />
	</bean>
 
</beans>
2.Register AutowiredAnnotationBeanPostProcessor
To enable@Autowired,you have to register'AutowiredAnnotationBeanPostProcessor',and you can do it in two ways:
1.Include
Add Spring context andin bean configration file.
<beans 
	//...
	xmlns:context="http://www.springframework.org/schema/context"
	//...
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	//...
 
	<context:annotation-config />
	//...
</beans>
フルexample
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:annotation-config />
 
	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
 
</beans>
2.Include AutowiredAnnotationBeanPostProcessor
Include‘AutowiredAnnotationBenPostProcessor’directly in bean configration file.
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
<bean 
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
 
	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
 
</beans>
3.@Autowired Examples
Now,you can atowired bean via@Autowired,and it can be appied on setter method,construtor or a field.
1.@Autowired setter method
package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class Customer 
{
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
 
	@Autowired
	public void setPerson(Person person) {
		this.person = person;
	}
}
2.@Autowired construtor
package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class Customer 
{
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
 
	@Autowired
	public Customer(Person person) {
		this.person = person;
	}
}
3.@Autowired field
package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class Customer 
{
	@Autowired
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}
The above example will atowired‘PersonBean’into Custoomer’s person property.
Run it
package com.mkyong.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	  new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
 
    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
 
    }
}
Output
Custoomer[action=buy、type=1、
person=Person[address=address 123,age=28,name=mkyong]
Dependency checking
By default,the@Autowired will perform the dependency checting to make sure the property has been wired properly.When Spring can't fina matching bean to wire,it will throw and excention.To fix,ou cashable@
package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class Customer 
{
	@Autowired(required=false)
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}
In the above example,if the Spring can’t find a matching bean,it will leave the person property unset.
@Qualfier
The@Qualfier annotation us ed to control which bean shound be autwire on a field.For example、bean configration file with two simillar person beans.
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
	<context:annotation-config />
 
	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean1" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
 
	<bean id="PersonBean2" class="com.mkyong.common.Person">
		<property name="name" value="mkyong2" />
		<property name="address" value="address 2" />
		<property name="age" value="28" />
	</bean>
 
</beans>
Will Spring know which bean shuld wire?
To fix it、you can use@Qualfier to aut wire a particular bean、for example、
package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
 
public class Customer 
{
	@Autowired
	@Qualifier("PersonBean1")
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}
It means,bean「PersonBean 1」is autwired into the Cusstomer’s person property.Read this full example–Spring Autowiring@Qualfier example