
- 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 基於構造器的自動裝配
此模式與byType非常相似,但它適用於建構函式引數。Spring容器查詢在XML配置檔案中autowire屬性設定為constructor的Bean。然後嘗試將其建構函式的引數與配置檔案中的一個Bean名稱進行匹配和連線。如果找到匹配項,它將注入這些Bean。否則,Bean將不會被連線。
例如,如果在配置檔案中將Bean定義設定為透過constructor自動裝配,並且它具有一個SpellChecker型別的引數的建構函式,則Spring會查詢名為SpellChecker的Bean定義,並使用它來設定建構函式的引數。您仍然可以使用<constructor-arg>標籤連線其餘引數。以下示例將說明此概念。
讓我們準備好一個可執行的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
步驟 | 描述 |
---|---|
1 | 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個com.tutorialspoint包。 |
2 | 如Spring Hello World 示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。 |
3 | 在com.tutorialspoint包下建立Java類TextEditor、SpellChecker和MainApp。 |
4 | 在src資料夾下建立Bean配置檔案Beans.xml。 |
5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,然後執行應用程式,如下所述。 |
以下是TextEditor.java檔案的內容:
package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; private String name; public TextEditor( SpellChecker spellChecker, String name ) { this.spellChecker = spellChecker; this.name = name; } public SpellChecker getSpellChecker() { return spellChecker; } public String getName() { return name; } 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/beans http://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" /> <constructor-arg value = "Generic Text Editor"/> </bean> <!-- Definition for spellChecker bean --> <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean> </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" autowire = "constructor"> <constructor-arg value = "Generic Text Editor"/> </bean> <!-- Definition for spellChecker bean --> <bean id = "SpellChecker" class = "com.tutorialspoint.SpellChecker"></bean> </beans>
建立原始碼和Bean配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside checkSpelling.
廣告