package com.h3.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.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="personbean" class="com.h3.common.person"> <property name="name" value="h3" /> <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.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="personbean" class="com.h3.common.person"> <property name="name" value="h3" /> <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.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="personbean" class="com.h3.common.person"> <property name="name" value="h3" /> <property name="address" value="address abc" /> <property name="age" value="29" /> </bean> </beans>
package com.h3.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; } }
package com.h3.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; } }
package com.h3.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 }
执行它
package com.h3.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[] {"applicationcontext.xml"}); customer cust = (customer)context.getbean("customerbean"); system.out.println(cust); } }
输出
customer [person=person [name=h3a], type=1, action=buy]
默认情况下,@autowired将执行相关检查,以确保属性已经装配正常。当spring无法找到匹配的bean装配,它会抛出异常。要解决这个问题,可以通过 @autowired 的“required”属性设置为false来禁用此检查功能。
package com.h3.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 }
<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.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="personbean1" class="com.h3.common.person"> <property name="name" value="h3-1" /> <property name="address" value="address-1" /> <property name="age" value="29" /> </bean> <bean id="personbean2" class="com.h3.common.person"> <property name="name" value="h3-2" /> <property name="address" value="address-2" /> <property name="age" value="28" /> </bean> </beans>
package com.h3.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 }
这意味着,“personbean1” bean被自动装配到customer的person属性。阅读下面完整的例子 – spring自动装配@qualifier实例