package com.tw511.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.tw511.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.tw511.common.Person"> <property name="name" value="yiibai" /> <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.tw511.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.tw511.common.Person"> <property name="name" value="yiibai" /> <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.tw511.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.tw511.common.Person"> <property name="name" value="yiibai" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
package com.tw511.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.tw511.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.tw511.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.tw511.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=YiibaiA], type=1, action=buy]
預設情況下,@Autowired將執行相關檢查,以確保屬性已經裝配正常。當Spring無法找到匹配的Bean裝配,它會丟擲異常。要解決這個問題,可以通過 @Autowired 的「required」屬性設定為false來禁用此檢查功能。
package com.tw511.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.tw511.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean1" class="com.tw511.common.Person"> <property name="name" value="yiibai-1" /> <property name="address" value="address-1" /> <property name="age" value="29" /> </bean> <bean id="PersonBean2" class="com.tw511.common.Person"> <property name="name" value="yiibai-2" /> <property name="address" value="address-2" /> <property name="age" value="28" /> </bean> </beans>
package com.tw511.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範例