
- 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 按名稱自動裝配
此模式指定按屬性名稱自動裝配。Spring 容器查詢在 XML 配置檔案中將auto-wire 屬性設定為byName 的 Bean。然後,它嘗試將自身的屬性與配置檔案中定義的同名 Bean 進行匹配和連線。如果找到匹配項,它將注入這些 Bean。否則,Bean 將不會被連線。
例如,如果 Bean 定義在配置檔案中設定為按名稱自動裝配,並且它包含一個spellChecker 屬性(即,它具有setSpellChecker(...) 方法),Spring 將查詢名為 spellChecker 的 Bean 定義,並使用它來設定該屬性。您仍然可以使用 <property> 標籤連線其餘屬性。以下示例將說明此概念。
讓我們準備好一個可用的 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 void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } 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"> <property name = "spellChecker" ref = "spellChecker" /> <property name = "name" 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 = "byName"> <property name = "name" value = "Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean> </beans>
建立原始檔和 Bean 配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside checkSpelling.列印
廣告