- 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 屬性注入依賴項的選項。
示例
以下示例顯示了一個類 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 static 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>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker" factory-method="getInstance"></bean>
</beans>
建立原始檔和 Bean 配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:
Inside SpellChecker getInstance. Inside SpellChecker constructor. Inside checkSpelling.
廣告