當容器調用帶有一組參數的類構造函數時,基于構造函數的 DI 就完成了,其中每個參數代表一個對其他類的依賴。
接下來,我們將通過示例來理解 Spring?基于構造函數的依賴注入。
示例:
下面的例子顯示了一個類 TextEditor,只能用構造函數注入來實現依賴注入。
讓我們用 Eclipse IDE 適當地工作,并按照以下步驟創建一個 Spring 應用程序。
步驟 | 描述 |
---|---|
1 | 創建一個名為 SpringExample 的項目,并在創建的項目中的 src 文件夾下創建包 com.tutorialspoint 。 |
2 | 使用 Add External JARs 選項添加必需的 Spring 庫,解釋見 Spring Hello World Example chapter. |
3 | 在 com.tutorialspoint 包下創建 Java類 TextEditor,SpellChecker 和 MainApp。 |
4 | 在 src 文件夾下創建 Beans 的配置文件 Beans.xml 。 |
5 | 最后一步是創建所有 Java 文件和 Bean 配置文件的內容并按照如下所示的方法運行應用程序。 |
這是 TextEditor.java 文件的內容:
package com.tutorialspoint;
public class TextEditor {private SpellChecker spellChecker;public TextEditor(SpellChecker spellChecker) {System.out.println("Inside TextEditor constructor." );this.spellChecker = spellChecker;}public void spellCheck() {spellChecker.checkSpelling();}
}
下面是另一個依賴類文件 SpellChecker.java 的內容:
package com.tutorialspoint;
public class SpellChecker {public SpellChecker(){System.out.println("Inside SpellChecker constructor." );}public void checkSpelling() {System.out.println("Inside checkSpelling." );}
}
以下是 MainApp.java 文件的內容:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");TextEditor te = (TextEditor) context.getBean("textEditor");te.spellCheck();}
}
下面是配置文件 Beans.xml 的內容,它有基于構造函數注入的配置:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Definition for textEditor bean --><bean id="textEditor" class="com.tutorialspoint.TextEditor"><constructor-arg ref="spellChecker"/></bean><!-- Definition for spellChecker bean --><bean id="spellChecker" class="com.tutorialspoint.SpellChecker"></bean></beans>
當你完成了創建源和 bean 配置文件后,讓我們開始運行應用程序。如果你的應用程序運行順利的話,那么將會輸出下述所示消息:
Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.
構造函數參數解析:
注釋:上面這個例子里,將依賴類?SpellChecker.java注入到TextEditor.java?文件。
如此,便稱為依賴注入。
如果存在不止一個參數時,當把參數傳遞給構造函數時,可能會存在歧義。要解決這個問題,那么構造函數的參數在 bean 定義中的順序就是把這些參數提供給適當的構造函數的順序就可以了。
考慮下面的類:
package x.y;
public class Foo {public Foo(Bar bar, Baz baz) {// ...}
}
下述配置文件工作順序:
<beans><bean id="foo" class="x.y.Foo"><constructor-arg ref="bar"/><constructor-arg ref="baz"/></bean><bean id="bar" class="x.y.Bar"/><bean id="baz" class="x.y.Baz"/>
</beans>
讓我們再檢查一下我們傳遞給構造函數不同類型的位置。考慮下面的類:
package x.y;
public class Foo {public Foo(int year, String name) {// ...}
}
如果你使用 type
屬性顯式的指定了構造函數參數的類型,容器也可以使用與簡單類型匹配的類型。例如:
<beans><bean id="exampleBean" class="examples.ExampleBean"><constructor-arg type="int" value="2001"/><constructor-arg type="java.lang.String" value="Zara"/></bean></beans>
最后并且也是最好的傳遞構造函數參數的方式,使用 index
屬性來顯式的指定構造函數參數的索引。下面是基于索引為 0 的例子,如下所示:
<beans><bean id="exampleBean" class="examples.ExampleBean"><constructor-arg index="0" value="2001"/><constructor-arg index="1" value="Zara"/></bean></beans>
最后,如果你想要向一個對象傳遞一個引用,你需要使用 標簽的 ref 屬性,如果你想要直接傳遞值,那么你應該使用如上所示的 value 屬性。