當容器調用一個無參的構造函數或一個無參的靜態 factory 方法來初始化你的 bean 后,通過容器在你的 bean 上調用設值函數,基于設值函數的 DI 就完成了。
下述例子顯示了一個類 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;
// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
在這里,你需要檢查設值函數方法的名稱轉換。要設置一個變量 spellChecker,我們使用 setSpellChecker() 方法,該方法與 Java POJO 類非常相似。讓我們創建另一個依賴類文件 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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.tutorialspoint.TextEditor"> <property name="spellChecker" ref="spellChecker"/> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
你應該注意定義在基于構造函數注入和基于設值函數注入中的 Beans.xml 文件的區別。唯一的區別就是在基于構造函數注入中,我們使用的是 標簽中的 元素,而在基于設值函數的注入中,我們使用的是 標簽中的 元素。
第二個你需要注意的點是,如果你要把一個引用傳遞給一個對象,那么你需要使用 標簽的 ref 屬性,而如果你要直接傳遞一個值,那么你應該使用 value 屬性。
當你完成了創建源和 bean 配置文件后,讓我們開始運行應用程序。如果你的應用程序運行順利的話,那么將會輸出下述所示消息:
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.
如果你有許多的設值函數方法,那么在 XML 配置文件中使用 p-namespace 是非常方便的。讓我們查看一下區別:
以帶有 標簽的標準 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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="john-classic" class="com.example.Person"> <property name="name" value="John Doe"/> <property name="spouse" ref="jane"/> </bean> <bean name="jane" class="com.example.Person"> <property name="name" value="John Doe"/> </bean> </beans>
上述 XML 配置文件可以使用 p-namespace 以一種更簡潔的方式重寫,如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="john-classic" class="com.example.Person" p:name="John Doe" p:spouse-ref="jane"/> </bean> <bean name="jane" class="com.example.Person" p:name="John Doe"/> </bean> </beans>
在這里,你不應該區別指定原始值和帶有 p-namespace 的對象引用。-ref 部分表明這不是一個直接的值,而是對另一個 bean 的引用。