Spring(二) @Autowired

Spring 2.5 引入了 @Autowired 註釋,它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。

在Spring 2.5以後,可以使用註解的方式定義Bean,較之於XML配置方式,註解配置方式的簡單性非常明顯,已經被廣泛接受,成爲一種趨勢,所以除非沒有辦法,否則我們都應用盡量採用註解的配置方式。




要實現我們要精簡程序的目的。需要這樣來處理:

* 在applicationContext.xml中加入:

  1. <!-- 該 BeanPostProcessor 將自動對標註 @Autowired 的 Bean 進行注入 -->
  2. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Spring 通過一個 BeanPostProcessor 對 @Autowired 進行解析,所以要讓 @Autowired 起作用必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。


* 修改在原來注入spirng容器中的bean的方法。
在域變量上加上標籤@Autowired,並且去掉 相應的get 和set方法

清單 6. 使用 @Autowired 註釋的 Boss.java

  1. package com.baobaotao;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class Boss {
  4. @Autowired
  5. private Car car;
  6. @Autowired
  7. private Office office;
  8. }

* 在applicatonContext.xml中 把原來 引用的<porpery >標籤也去掉。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <!-- 該 BeanPostProcessor 將自動起作用,對標註 @Autowired 的 Bean 進行自動注入 -->
  7. <bean class="org.springframework.beans.factory.annotation.
  8. AutowiredAnnotationBeanPostProcessor"/>
  9. <!-- 移除 boss Bean 的屬性注入配置的信息 -->
  10. <bean id="boss" class="com.baobaotao.Boss"/>
  11. <bean id="office" class="com.baobaotao.Office">
  12. <property name="officeNo" value="001"/>
  13. </bean>
  14. <bean id="car" class="com.baobaotao.Car"scope="singleton">
  15. <property name="brand" value=" 紅旗 CA72"/>
  16. <property name="price" value="2000"/>
  17. </bean>
  18. </beans>

這樣,當 Spring 容器啓動時,AutowiredAnnotationBeanPostProcessor 將掃描 Spring 容器中所有 Bean,當發現 Bean 中擁有 @Autowired 註釋時就找到和其匹配(默認按類型匹配)的 Bean,並注入到對應的地方中去。

按照上面的配置,Spring 將直接採用 Java 反射機制對 Boss 中的 car 和 office 這兩個私有成員變量進行自動注入。所以對成員變量使用 @Autowired 後,您大可將它們的 setter 方法(setCar() 和 setOffice())從 Boss 中刪除。

當然,您也可以通過 @Autowired 對方法或構造函數進行標註,如果構造函數有兩個入參,分別是 bean1 和 bean2,@Autowired 將分別尋找和它們類型匹配的 Bean,將它們作爲 CountryService (Bean1 bean1 ,Bean2 bean2) 的入參來創建 CountryService Bean。來看下面的代碼: 對方法

  1. package com.baobaotao;
  2. public class Boss {
  3. private Car car;
  4. private Office office;
  5. @Autowired
  6. public void setCar(Car car) {
  7. this.car = car;
  8. }
  9. @Autowired
  10. public void setOffice(Office office) {
  11. this.office = office;
  12. }
  13. }

這時,@Autowired 將查找被標註的方法的入參類型的 Bean,並調用方法自動注入這些 Bean。而下面的使用方法則對構造函數進行標註:

  1. package com.baobaotao;
  2. public class Boss {
  3. private Car car;
  4. private Office office;
  5. @Autowired
  6. public Boss(Car car ,Office office){
  7. this.car = car;
  8. this.office = office ;
  9. }
  10. }

由於 Boss() 構造函數有兩個入參,分別是 car 和 office,@Autowired 將分別尋找和它們類型匹配的 Bean,將它們作爲 Boss(Car car ,Office office) 的入參來創建 Boss Bean。  





@Autowired 默認是按照byType進行注入的,但是當byType方式找到了多個符合的bean,又是怎麼處理的?


經過一些代碼的測試,我發現,Autowired默認先按byType,如果發現找到多個bean,則,又按照byName方式比對,如果還有多個,則報出異常。

例子:

@Autowired
private ExamUserMapper examUserMapper; - ExamUserMapper是一個接口


1. spring先找類型爲ExamUserMapper的bean

2. 如果存在且唯一,則OK;

3. 如果不唯一,在結果集裏,尋找name爲examUserMapper的bean。因爲bean的name有唯一性,所以,到這裏應該能確定是否存在滿足要求的bean了


@Autowired也可以手動指定按照byName方式注入,使用@Qualifier標籤,例如:
@Autowired() @Qualifier ( "baseDao" )

因爲bean的name具有唯一性,理論上是byName會快一點,但spring默認使用byType的方式注入,讓我很迷惑,確定不了哪一個真的快。具體到實際應用,感覺差別不大,就看大家的習慣





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章