In Spring, you can use @Autowired annotation to auto wire bean on the setter method, constructor or a field. Moreover, it can autowired property in a particular bean.
Note
The @Autowired annotation is auto wire the bean by matching data type.
1. Beans
A customer bean, and declared in bean configuration file. Later, you will use “@Autowired” to auto 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 <context:annotation-config />
Add Spring context and <context:annotation-config /> in bean configuration file.Full example,<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>
<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 ‘AutowiredAnnotationBeanPostProcessor’ directly in bean configuration 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 autowired bean via @Autowired, and it can be applied on setter method, constructor 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 autowired ‘PersonBean’ into Customer’s person property. Run it
Outputpackage 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); } }
Customer [action=buy, type=1, person=Person [address=address 123, age=28, name=mkyong]]
No comments:
Post a Comment