- 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 - 内部 Bean Setter 注入
正如您所知,Java 内部类是在其他类的范围内定义的,类似地,**内部 Bean** 是在另一个 Bean 的范围内定义的 Bean。因此,在<property/>或<constructor-arg/>元素内部的<bean/>元素称为内部 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>
示例
以下示例显示了一个TextEditor类,它只能使用纯基于 Setter 的注入进行依赖注入。
让我们更新在Spring DI - 创建项目章节中创建的项目。我们正在添加以下文件 -
TextEditor.java - 包含 SpellChecker 作为依赖项的类。
SpellChecker.java - 依赖类。
MainApp.java - 运行和测试的主要应用程序。
以下是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("applicationcontext.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
以下是配置文件applicationcontext.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.
广告