package com.zyiz.netmon; 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.zyiz.netmon.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz" /> <property name="address" value="address 123" /> <property name="age" value="28" /> </bean> </beans>
<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.zyiz.netmon.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
<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.zyiz.netmon.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
package com.zyiz.netmon; 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; } }
package com.zyiz.netmon; 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; } }
package com.zyiz.netmon; import org.springframework.beans.factory.annotation.Autowired; public class Customer { @Autowired private Person person; private int type; private String action; //getter and setter methods }
执行它
package com.zyiz.netmon; 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[] {"applicationContext.xml"}); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
输出
Customer [person=Person [name=zyizA], type=1, action=buy]
默认情况下,@Autowired将执行相关检查,以确保属性已经装配正常。当Spring无法找到匹配的Bean装配,它会抛出异常。要解决这个问题,可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能。
package com.zyiz.netmon; 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 }
<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.zyiz.netmon.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean1" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz-1" /> <property name="address" value="address-1" /> <property name="age" value="29" /> </bean> <bean id="PersonBean2" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz-2" /> <property name="address" value="address-2" /> <property name="age" value="28" /> </bean> </beans>
package com.zyiz.netmon; 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 }
这意味着,“PersonBean1” bean被自动装配到customer的person属性。阅读下面完整的例子 – Spring自动装配@Qualifier实例