在上一篇 Spring在XML自動裝配 示例中,它會匹配當前Spring容器任何bean的屬性自動裝配。在大多數情況下,你可能只需要在特定的 bean 自動裝配屬性。
在Spring中,可以使用 @Autowired 注解通過setter方法,構造函數或字段自動裝配Bean。此外,它可以在一個特定的bean屬性自動裝配。
注 @Autowired注解是通過匹配數據類型自動裝配Bean。
請參見下麵的完整的例子來演示如何使用@Autowired。
1. Beans
一個 Customer bean 在bean配置檔中聲明。稍後,您將使用 “@Autowired” 來自動裝配一個Person bean。
package com.xuhuhu.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.xuhuhu.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.xuhuhu.common.Person"> <property name="name" value="zaixian" /> <property name="address" value="address 123" /> <property name="age" value="28" /> </bean> </beans>
2. 註冊AutowiredAnnotationBeanPostProcessor
要啟用@Autowired,必須註冊“AutowiredAnnotationBeanPostProcessor',你可以用兩種方式做到這一點:
1. Include <context:annotation-config />
添加 Spring 上下文和<context:annotation-config />在bean配置檔中。
<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.xuhuhu.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.xuhuhu.common.Person"> <property name="name" value="zaixian" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
2. 包含 AutowiredAnnotationBeanPostProcessor
直接在bean配置檔包含“AutowiredAnnotationBeanPostProcessor”。
<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.xuhuhu.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.xuhuhu.common.Person"> <property name="name" value="zaixian" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
3. @Autowired示例
現在,你可以通過 @Autowired 自動裝配 bean,它可以在 setter 方法,構造函數或字段中使用。
1. @Autowired setter 方法
package com.xuhuhu.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 構造方法
package com.xuhuhu.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 字段
package com.xuhuhu.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 }
上面的例子會自動裝配“PersonBean”到Customer的person屬性。
執行它
package com.xuhuhu.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=zaixianA], type=1, action=buy]
依賴檢查
默認情況下,@Autowired將執行相關檢查,以確保屬性已經裝配正常。當Spring無法找到匹配的Bean裝配,它會拋出異常。要解決這個問題,可以通過 @Autowired 的“required”屬性設置為false來禁用此檢查功能。
package com.xuhuhu.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 }
在上面的例子中,如果Spring不能找到一個匹配的Bean,person屬性將不設定。
@Qualifier
@Qualifier注解我們用來控制bean應在字段上自動裝配。例如,具有兩個類似的 person bean 配置檔。
<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.xuhuhu.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean1" class="com.xuhuhu.common.Person"> <property name="name" value="zaixian-1" /> <property name="address" value="address-1" /> <property name="age" value="29" /> </bean> <bean id="PersonBean2" class="com.xuhuhu.common.Person"> <property name="name" value="zaixian-2" /> <property name="address" value="address-2" /> <property name="age" value="28" /> </bean> </beans>
Spring知道哪個 bean 應當裝配?
為了解決這個問題,可以使用 @Qualifier 自動裝配一個特定的 bean,例如,
package com.xuhuhu.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實例
總結
這@Autowired注解非常靈活,功能強大,絕對比bean配置檔的“autowire”屬性要更好。
上一篇:
Spring通過自動檢測自動裝配
下一篇:
Spring自動裝配@Qualifier實例