- 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 - 地图构造器
- Spring DI - 地图引用构造函数
- 基于 Setter 的注入示例
- Spring DI - 基于 Setter
- Spring DI - 内部 Bean Setter
- Spring DI - 集合设置器
- Spring DI - 系列参考设置器
- Spring DI - 地图设置器
- Spring DI - 地图参考设置器
- 自动装配示例
- Spring DI - 自动装配
- Spring DI - 自动装配 ByName
- Spring DI - 按类型自动装配
- Spring DI - 自动装配构造函数
- 工厂方法
- Spring DI - 静态工厂
- Spring DI - 非静态工厂
- Spring DI 有用资源
- Spring DI - 快速指南
- Spring DI - 有用的资源
- Spring DI - 讨论
Spring DI - 静态工厂
Spring 提供了使用工厂方法属性注入依赖项的选项。
例子
以下示例显示了只能使用纯基于 setter 的注入进行依赖项注入的TextEditor类。
让我们更新一下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.