1. 程式人生 > >學習筆記:Spring中default-autowire與autowire區別

學習筆記:Spring中default-autowire與autowire區別

  default-autowire與autowire主要用於Spring的IOC的註解注入,明白兩者的區別和用法將使你的開發事半功倍。
  Spring 提供了Resource、Autowired這兩個註解用於注入,另外在xml配置檔案中,beans標籤下有一個引數default-autowire用來設定預設的注入型別。

default-autowire和autowire的可選值

可選值 功能說明
no 預設不使用autowiring。 必須顯示的使用”“標籤明確地指定bean。
byName 根據屬性名自動裝配。此選項將檢查容器並根據名字查詢與屬性完全一致的bean,並將其與屬性自動裝配。
byType 如果容器中存在一個與指定屬性型別相同的bean,那麼將與該屬性自動裝配。如果存在多個該型別的bean,那麼將會丟擲異常,並指出不能使用byType方式進行自動裝配。若沒有找到相匹配的bean,則什麼事都不發生,屬性也不會被設定。如果你不希望這樣,那麼可以通過設定 dependency-check=”objects”讓Spring丟擲異常。
constructor 與byType的方式類似,不同之處在於它應用於構造器引數。如果在容器中沒有找到與構造器引數型別一致的bean,那麼將會丟擲異常。
autodetect 通過bean類的自省機制(introspection)來決定是使用constructor還是byType方式進行自動裝配。如果發現預設的構造器,那麼將使用byType方式。

beans標籤設定default-autowire引數

  在spring的配置檔案中可以參照如下設定default-autowire引數

<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.xsd"
default-autowire="byName">

  在beans標籤設定default-autowire=”byName”後,在spring容器例項化bean(包含xml配置bean和註解方式bean)時,該bean內部的欄位會自動根據byName裝載spring容器裡的bean。如下:
Spring配置檔案中

<bean id="teacher" class="com.haiwi.spring.Teacher"></bean>
<bean id="student" class="com.haiwi.spring.Student">
     <property name="stu_name" value="Tom" />
</bean>

Student類

public class Teacher {
    String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class Student {
    private Teacher teacher;
    private String stu_name;
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public String getStu_name() {
        return stu_name;
    }
    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }
}

  此時spring容器啟動載入bean時候由於在beans標籤設定default-autowire=”byName”,那麼在載入Student的Bean時候,會把字串”Tom”注入到Student類的stu_name欄位,而由於配置了自動注入所有在注入teacher欄位時候,會掃描容器裡id為teacher的Bean注入到該欄位中,而不用再在id為student的Bean配置了。

autowire引數的用法

  autowire可用在兩次:XML中配置Bean的引數和@Autowired註解注入。兩者都是把對應的類實現自動注入。
(1)XML中配置Bean的引數  
Autowire配置的可選值依然如上述表格中的說明,XML配置檔案如下:

<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.xsd">
<bean id="teacher" class="com.haiwi.spring.Teacher"></bean>
<bean id="student" class="com.haiwi.spring.Student" autowire="byName">
             <property name="stu_name" value="Tom" />
</bean>
…..
</beans>

  同樣,spring容器啟動載入bean時候,在載入到Student的Bean時候,會把字串”Tom”注入到Student類的stu_name欄位,而同時由於對這個類配置了autowire=”byName”引數,所以會掃描spring容器中id為teacher的Bean然後注入到該類的teacher欄位中。
(2)@Autowired註解注入 
  @Autowired往往用在類中註解注入,在配置xml需要配置才能使用@Autowired標識

<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/context http://www.springframework.org/schema/beans/spring-beans.xsd">
    <context:annotation-config />
    <bean id="teacher" class="com.haiwi.spring.Teacher"></bean>
    <bean id="student" class="com.haiwi.spring.Student" autowire="byName">
        <property name="stu_name" value="Tom" />
    </bean>
    …..
</beans>

 在類中使用@Autowired標識注入欄位

@Autowired
@Qualifier("teacher")
private Teacher teacher;
public void setTeacher(Teacher teacher) {
   this.teacher = teacher;
}