- 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 基于Setter的依赖注入
基于Setter的DI是通过容器在调用无参构造函数或无参静态工厂方法实例化bean之后,调用bean上的setter方法来完成的。
示例
以下示例展示了一个名为TextEditor的类,它只能使用纯基于setter的注入进行依赖注入。
让我们准备好一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序:
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中src文件夹下创建一个名为com.tutorialspoint的包。 |
| 2 | 使用添加外部JAR选项添加所需的Spring库,如Spring Hello World示例章节中所述。 |
| 3 | 在com.tutorialspoint包下创建Java类TextEditor、SpellChecker和MainApp。 |
| 4 | 在src文件夹下创建Beans配置 文件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();
}
}
在这里,您需要检查setter方法的命名约定。要设置变量spellChecker,我们使用setSpellChecker()方法,这与Java POJO类非常相似。让我们创建另一个依赖类文件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();
}
}
以下是Beans.xml配置文件,其中包含基于setter的注入的配置:
<?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">
<property name = "spellChecker" ref = "spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
您应该注意在基于构造函数的注入和基于setter的注入中定义的Beans.xml文件中的区别。唯一的区别是在<bean>元素内部,我们对基于构造函数的注入使用了<constructor-arg>标签,对基于setter的注入使用了<property>标签。
第二个需要注意的重要事项是,如果您传递的是对对象的引用,则需要使用<property>标签的ref属性,如果您直接传递值,则应使用value属性。
创建完源代码和bean配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.
使用p命名空间的XML配置
如果您有很多setter方法,那么在XML配置文件中使用p命名空间会很方便。让我们检查一下区别:
让我们考虑一个使用<property>标签的标准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">
<bean id = "john-classic" class = "com.example.Person">
<property name = "name" value = "John Doe"/>
<property name = "spouse" ref = "jane"/>
</bean>
<bean name = "jane" class = "com.example.Person">
<property name = "name" value = "John Doe"/>
</bean>
</beans>
以上XML配置可以使用p命名空间以更简洁的方式重写,如下所示:
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p = "http://www.springframework.org/schema/p"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "john-classic" class = "com.example.Person"
p:name = "John Doe"
p:spouse-ref = "jane"/>
</bean>
<bean name =" jane" class = "com.example.Person"
p:name = "John Doe"/>
</bean>
</beans>
在这里,您应该注意使用p命名空间指定基本值和对象引用时的区别。-ref部分表示这不是一个直接的值,而是对另一个bean的引用。