
- Spring DI 教程
- Spring DI - 首頁
- Spring DI - 概述
- Spring DI - 環境設定
- Spring DI - IOC 容器
- Spring 依賴注入
- Spring DI - 建立專案
- 基於建構函式的注入示例
- Spring DI - 基於建構函式
- Spring DI - 內部 Bean 建構函式
- Spring DI - 集合建構函式
- Spring DI - 集合引用建構函式
- Spring DI - Map 建構函式
- Spring DI - Map 引用建構函式
- 基於 Setter 的注入示例
- Spring DI - 基於 Setter
- Spring DI - 內部 Bean Setter
- Spring DI - 集合 Setter
- Spring DI - 集合引用 Setter
- Spring DI - Map Setter
- Spring DI - Map 引用 Setter
- 自動裝配示例
- Spring DI - 自動裝配
- Spring DI - 按名稱自動裝配
- Spring DI - 按型別自動裝配
- Spring DI - 建構函式自動裝配
- 工廠方法
- Spring DI - 靜態工廠
- Spring DI - 非靜態工廠
- Spring DI 有用資源
- Spring DI - 快速指南
- Spring DI - 有用資源
- Spring DI - 討論
Spring DI - 非靜態工廠
對於非靜態工廠方法,Spring 提供了一種使用 factory-method 和 factory-bean 屬性來注入依賴項的選項。
示例
以下示例顯示了一個 TextEditor 類,它只能使用純基於 Setter 的注入進行依賴注入。
讓我們更新在Spring DI - 建立專案章節中建立的專案。我們將新增以下檔案:
TextEditor.java - 包含 SpellChecker 作為依賴項的類。
SpellChecker.java - 依賴類。
MainApp.java - 執行和測試的主要應用程式。
以下是 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 的內容:
此類的建構函式是私有的。因此,它的物件不能透過其他物件使用 new 運算子直接建立。它有一個非靜態工廠方法來獲取例項。
package com.tutorialspoint; public class SpellChecker { private SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public SpellChecker getInstance() { System.out.println("Inside SpellChecker getInstance." ); return new SpellChecker(); } 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("applicationcontext.xml"); TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); } }
以下是配置檔案 applicationcontext.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> <bean id = "spellCheckFactory" class = "com.tutorialspoint.SpellChecker"></bean> <!-- Definition for spellChecker bean --> <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker" factory-method="getInstance">< factory-bean="spellCheckFactory"/bean> </beans>
輸出
建立原始檔和 Bean 配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
Inside SpellChecker constructor. Inside SpellChecker getInstance. Inside SpellChecker constructor. Inside checkSpelling.
廣告