
- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境設定
- Spring - Hello World示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - 使用Spring框架的AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring 基於Setter的依賴注入
基於Setter的DI是透過容器在呼叫無參建構函式或無參靜態工廠方法例項化bean之後,呼叫bean上的setter方法來完成的。
示例
以下示例展示了一個名為TextEditor的類,它只能使用純基於setter的注入進行依賴注入。
讓我們準備好一個可用的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
步驟 | 描述 |
---|---|
1 | 建立一個名為SpringExample的專案,並在建立的專案中src資料夾下建立一個名為com.tutorialspoint的包。 |
2 | 使用新增外部JAR選項新增所需的Spring庫,如Spring Hello World示例章節中所述。 |
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(); } }
在這裡,您需要檢查setter方法的命名約定。要設定變數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配置檔案,其中包含基於setter的注入的配置:
<?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>
您應該注意在基於建構函式的注入和基於setter的注入中定義的Beans.xml檔案中的區別。唯一的區別是在<bean>元素內部,我們對基於建構函式的注入使用了<constructor-arg>標籤,對基於setter的注入使用了<property>標籤。
第二個需要注意的重要事項是,如果您傳遞的是對物件的引用,則需要使用<property>標籤的ref屬性,如果您直接傳遞值,則應使用value屬性。
建立完原始碼和bean配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.
使用p名稱空間的XML配置
如果您有很多setter方法,那麼在XML配置檔案中使用p名稱空間會很方便。讓我們檢查一下區別:
讓我們考慮一個使用<property>標籤的標準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名稱空間以更簡潔的方式重寫,如下所示:
<?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名稱空間指定基本值和物件引用時的區別。-ref部分表示這不是一個直接的值,而是對另一個bean的引用。