Spring的依賴注入及三種配置方式(依賴注入的兩種形式)

依賴注入的兩種形式

1.1構造方法注入

LowerAction.class

public class LowerAction implements Action {
    private String prefix;
    private String message;

    public  LowerAction(){}

    public  LowerAction(String prefix, String message){
        this.prefix = prefix;
        this.message = message;
    }
    public String getPrefix() {
        return prefix;
    }
    public String getMessage() {
        return message;
    }
    public void setPrefix(String string) {
        prefix = string;
    }
    public void setMessage(String string) {
        message = string;
    }
    public void execute(String str) {
        System.out.println((getPrefix() + ", " + getMessage() + ", " + str).toLowerCase());
    }
}

ApplicationContext.xml中的TheAction2的Bean

    <bean id="TheAction2" class="com.example.service.LowerAction">
        <constructor-arg index="0">
            <value>Hi</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value>Good Afternoon</value>
        </constructor-arg>
    </bean>

測試函數

    @Test
    public void test5() {
        String XML = "file:src/applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(XML);

        LowerAction lowerAction=(LowerAction) ctx.getBean("TheAction2");
        System.out.println(lowerAction.getPrefix());
        System.out.println(lowerAction.getMessage());

    }

測試結果


測試結果

實驗中想到的問題:構造注入只能通過index索引匹配嗎?
還有類型匹配

<bean id="TheAction4" class="com.example.service.LowerAction">
        <constructor-arg type="java.lang.String">
            <value>Hi</value>
        </constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>Wushuohan</value>
        </constructor-arg>
    </bean>

以及參數名傳值

<bean id="TheAction6" class="com.example.service.LowerAction">
        <constructor-arg name="prefix" value="Hi">
        </constructor-arg>
        <constructor-arg type="message" value="Wushuohan">
        </constructor-arg>
    </bean>

測試結果如下

測試結果

Setter()方法注入

ApplicationContext.xml中的TheAction1的Bean

<bean id="TheAction1" class="com.example.service.LowerAction">
        <property name="prefix">
            <value>Hi</value>
        </property>
        <property name="message">
            <value>Good Morning</value>
        </property>
    </bean>

測試函數

    @Test
    public void test4() {
        String XML = "file:src/applicationContext.xml";
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(XML);
        LowerAction lowerAction=(LowerAction) ctx.getBean("TheAction1");
        System.out.println(lowerAction.getPrefix());
        System.out.println(lowerAction.getMessage());
    }

測試結果如下


測試結果

實驗中想到的問題:Setter()注入能不能沒有構造函數?
注釋掉LowerAction的構造函數

public class LowerAction implements Action {
    private String prefix;
    private String message;

    public void execute(String str) {
        System.out.println((getPrefix() + ", " + getMessage() + ", " + str).toLowerCase());
    }
}

運行后報錯

報錯

報錯原因:TheAction2沒有構造函數
TheAction2沒有了構造函數

將這個Bean暫時刪除后,運行成功:
運行成功

以上的測試說明了Setter()注入可以沒有構造函數,但是構造注入必須有構造函數。


本章總結

對于依賴關系無需變化的注入,盡量采用構造注入。而其他的依賴關系的注入,則考慮采用設值注入。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,206評論 2 7
  • 文章作者:Tyan博客:noahsnail.com 3.4 依賴 標準企業應用不會由一個對象(或Spring用語中...
    SnailTyan閱讀 1,210評論 0 1
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 依賴注入簡介 依賴注入是一個很常用的詞。Java新手常會寫出如下的代碼,直接在寫一個類的時候讓它自己初始化自己。但...
    樂百川閱讀 2,593評論 3 10
  • 01 咪蒙,大概三四線城市的女生們,很多人都有聽說過咪蒙這個名字,其實咪蒙是我的女神,我所敬佩咪蒙寫的不是她的毒雞...
    王冊3閱讀 1,378評論 27 21