
- 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 - 注入內部 Bean
正如您所知,Java 內部類是在其他類的作用域內定義的,類似地,**內部 Bean** 是在另一個 Bean 的作用域內定義的 Bean。因此,<bean/> 元素位於 <property/> 或 <constructor-arg/> 元素內時,稱為內部 Bean,如下所示。
<?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 = "outerBean" class = "..."> <property name = "target"> <bean id = "innerBean" class = "..."/> </property> </bean> </beans>
示例
讓我們準備好正在使用的 Eclipse IDE,並按照以下步驟建立一個 Spring 應用程式:
步驟 | 描述 |
---|---|
1 | 建立一個名為 SpringExample 的專案,並在建立的專案中的 src 資料夾下建立一個名為 com.tutorialspoint 的包。 |
2 | 使用 新增外部 JAR 選項新增所需的 Spring 庫,如 Spring Hello World 示例 章節中所述。 |
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; // 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.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(); } }
以下是 Bean 配置檔案 Beans.xml,其中包含基於 setter 的注入的配置,但使用**內部 Bean**:
<?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 using inner bean --> <bean id = "textEditor" class = "com.tutorialspoint.TextEditor"> <property name = "spellChecker"> <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"/> </property> </bean> </beans>
建立原始檔和 Bean 配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.
廣告