對于spring中IOC和DI的理解

1. 概述

IOC是一種設(shè)計思想,旨在實現(xiàn)調(diào)用類和實現(xiàn)類的松耦合,調(diào)用類只依賴接口,在編譯階段并不知道具體使用哪個實現(xiàn)類,而是將這個控制權(quán)交給第三方容器,在運行時由容器將實例變量(具體的實現(xiàn)類)注入到對象中(調(diào)用類)。
DI是具體的實現(xiàn)技術(shù),是指由第三方組件負責(zé)將實例變量(實現(xiàn)類)傳入到對象(調(diào)用類)中去。

2.IOC舉例

先看一個非IOC的方式:

public interface Animal {
    public void perform();
}
public class Tiger implements Animal {
    @Override
    public void perform() {
        System.out.println("A tiger is performing!");
    }

    @Override
    public String toString() {
        return "I'm a tiger";
    }
}
public class Zoo {
    private Animal animal;

    public Zoo() {
        this.animal = new Tiger();
    }

    public void perform(){
        animal.perform();
    }

    @Override
    public String toString() {
        if (animal == null) {
            return null;
        }
        return animal.toString();
    }
}

Zoo在構(gòu)造函數(shù)中自行創(chuàng)建了Tiger的示例,這使得Zoo和Tiger緊密地耦合在了一起,擴展性會很差(比如Zoo需要一個Lion進行表演)。
如果使用IOC的方式,Zoo沒有自行創(chuàng)建Animal實例,而是在構(gòu)造的時候把Animal實例作為構(gòu)造器參數(shù)傳入。代碼如下:

public class Zoo {
    private Animal animal;

    // Animal被注入進來
    public Zoo(Animal animal) {
        this.animal = animal;
    }

    public void perform(){
        animal.perform();
    }

    @Override
    public String toString() {
        if (animal == null) {
            return null;
        }
        return animal.toString();
    }
}

3.DI的兩種方式

3.1構(gòu)造器注入

構(gòu)造器注入是在Bean構(gòu)造過程中執(zhí)行的

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
 
    <bean id="tiger" class="org.xrq.action.by.Tiger" />
     
    <bean id="zoo" class="org.xrq.action.by.Zoo">
        <constructor-arg  ref="tiger" />
    </bean>
         
</beans> 

3.2 Setter注入

Setter注入是在Bean實例創(chuàng)建完畢之后執(zhí)行,如下:

public class Zoo {
     
    private Animal animal;
     
    public Animal getAnimal() {
        return animal;
    }
 
    public void setAnimal(Animal animal) {
        this.animal = animal;
    }

   public void perform(){
        animal.perform();
    }
 
    @Override
    public String toString() {
        if (animal == null) {
            return null;
        }
        return animal.toString();
    }
     
}
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
 
    <bean id="tiger" class="org.xrq.action.by.Tiger" />
     
    <bean id="zoo" class="org.xrq.action.by.Zoo">
        <property name="animal" ref="tiger" />
    </bean>
         
</beans>   

3.3 思考

  1. Spring引入Autowire(自動裝配)機制就是為了解決<bean>標簽下<property>標簽或<constructor-arg>標簽過多的問題
  2. 對于強依賴,可使用構(gòu)造器注入,對于弱依賴,推薦使用Setter注入

4. 參考文章

http://www.importnew.com/13619.html
http://www.importnew.com/24731.html
https://my.oschina.net/u/3759357/blog/1825620

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容