Spring的依賴注入 DI

依賴注入(DI)和控制反轉(IoC)是從不同的角度描述的同一件事情,就是指通過引入IoC容器,利用依賴關係注入的方式,實現對象之間的解耦

依賴注入方式:
1、構造方法注入
2、setter方法注入
3、接口注入
4、複雜類型(集合)注入

代碼

//Computer類:
public class Computer {

    private String type;//電腦型號
    private OperatingSystem operatingSystem;//操作系統
        
    public Computer() {}
    public Computer(String type, OperatingSystem operatingSystem) {
        this.type = type;
        this.operatingSystem = operatingSystem;
    }
//此處略去getter setter toString方法
}//OperatingSystem類(這是Computer的一個屬性:操作系統)

public class OperatingSystem {
    private String name;//操作系統名字,例如Windows、macOS
  
  	//操作系統版本,例如,當name爲Windows,version爲10的時候,就是win10
    private String version;

    public OperatingSystem(){}
    public OperatingSystem(String name, String version) {
        this.name = name;
        this.version = version;
    }
    //此處略去getter setter toString方法

}

1.構造方法注入(一定要寫對應的構造方法!)

​ 此方式使用的是bean標籤內部的標籤進行依賴注入。

​ 以如下構造方法爲例

public Computer(String type, OperatingSystem operatingSystem) {
    this.type = type;
    this.operatingSystem = operatingSystem;
}

寫法1(不推薦,不常用):

<bean id="operatingSystem" class="di.OperatingSystem">
    <constructor-arg index="0" value="macOS"/>
    <constructor-arg index="1" value="Mojave"/>
</bean>
<bean id="computer" class="di.Computer">
    <constructor-arg index="0" value="MacBook Pro 2019"/>
    <constructor-arg index="1" ref="operatingSystem"/>
</bean>

​標籤的寫法還是和之前一樣,唯一不同的是有了標籤體,標籤體內部就是用來進行屬性設置(依賴注入)的。

每個constructor-arg標籤代表構造方法中的一個參數;

屬性index代表參數的順序,從0開始,index=“0”代表的是構造方法的String type這個參數,index=“1”代表的是OperatingSystem這個參數;

​屬性value代表的是這個參數要傳入的值,例如type這個參數對應的是macOS,version是Mojave,一般來說,基本類型和String類型要用value屬性來傳值。

由於在Computer類中我們引用了OperatingSystem這個對象,所以需要先定義好OperatingSystem這個bean,以供我們將其注入到computer這個bean中,其實OperatingSystem就是Computer的一個依賴。而在第二個bean中,有一個ref屬性,這個用來傳value傳不了的值,ref在本例中的值爲operatingSystem這個bean的id。

寫法2(推薦寫法,但不常用):

<bean id="operatingSystem" class="di.OperatingSystem">
    <constructor-arg name="name" value="macOS"/>
    <constructor-arg name="version" value="Mojave"/>
</bean>

<bean id="computer" class="di.Computer">
    <constructor-arg name="type" value="MacBook Pro 2019"/>
    <constructor-arg name="operatingSystem" ref="operatingSystem"/>
</bean>

​ 其他地方與寫法1一樣,唯一區別就是將寫法1中的index屬性換成了name屬性,這樣直接通過參數名來確定,清晰明瞭,不容易出錯。

2.setter方法注入(一定要寫好空參構造和所有屬性的setter方法!!!)

​ 此方式使用的是bean標籤內部的屬性進行依賴注入。

​ 以如下構造方法爲例

public Computer(String type, OperatingSystem operatingSystem) {
    this.type = type;
    this.operatingSystem = operatingSystem;
}

​ 注入:

<bean id="operatingSystem" class="di.OperatingSystem">
    <property name="name" value="macOS"/>
    <property name="version" value="Mojave"/>
</bean>
<bean id="computer" class="di.Computer">
    <property name="type" value="MacBook Pro 2019"/>
    <property name="operatingSystem" ref="operatingSystem"/>
</bean>

​ 其實與構造函數注入的第二種方式差不多,只不過是把換成了

3.接口注入(沒用,不說了)

4.集合注入
​注意:下文中的value標籤,當需要設的值是自己定義的bean時,要用將標籤替換。

​爲Computer類新增屬性keys,表示鍵盤上的鍵的集合。

1.數組與List(可互相替換)
​ 其實Spring中將Array和List等同起來了。

​ 寫法1(constructor-arg + array):

<bean id="computer" class="di.Computer">
    <constructor-arg name="keys">
        <array>
            <value>a</value>
            <value>b</value>
            <value>c</value>
        </array>
    </constructor-arg>
</bean>

​ 寫法2(constructor-arg + list):

<bean id="computer" class="di.Computer">
    <constructor-arg name="keys">
            <list>
                <value>a</value>
                <value>b</value>
            </list>
    </constructor-arg>
</bean>

​ 寫法3(property + array):

<bean id="computer" class="di.Computer">
    <property name="keys">
            <array>
                <value>a</value>
                <value>b</value>
            </array>
    </property>
</bean>

​ 寫法4(property + list):

<bean id="computer" class="di.Computer">
    <property name="keys">
            <list>
                <value>a</value>
                <value>b</value>
            </list>
    </property>
</bean>

2.Set
​ 寫法1(constructor-arg):

<bean id="computer" class="di.Computer">
    <constructor-arg name="keys">
            <set>
                <value>a</value>
                <value>b</value>
            </set>
    </constructor-arg>
</bean>

​ 寫法2(property):

<bean id="computer" class="di.Computer">
    <property name="keys">
            <set>
                <value>a</value>
                <value>b</value>
            </set>
    </property>
</bean>

3.Map
​爲Computer類新增屬性map<String,String> sofewares,key表示軟件,value表示軟件安裝目錄

​爲Computer類新增屬性map<String,Singer> songs,key表示歌曲名,value表示歌手對象

​寫法1(property):

<bean id="singerEdSheeran" class="di.Singer">
    <property name="name" value="Ed Sheeran"/>
</bean>

<bean id="computer" class="di.Computer">
    <property name="softwares">
            <map>
                <entry key="WeChat" value="/Users/...."/>
                <entry key="Intellij Idea" value="/usr/local/...."/>
            </map>
    </property>
    <property name="songs">
        <map>
            <entry key="Shape of You" value-ref="singerEdSheeran"/>
            <entry key="I dont care" value-ref="singerEdSheeran"/>
        </map>
    </property>
</bean>

​ 寫法2(將寫法1的property換成constructor-arg即可)

​ 注意,如果key或value要引用其他bean,那麼對應地需要將換成或將換成

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