基于 Spring Setter 的依赖注入
基于 Setter 的 DI 是通过容器在调用无参数构造函数或无参数 static factory 方法来实例化 bean 后调用 bean 上的 setter 方法来完成的。
示例
下面的例子展示了一个类 TextEditor,它只能使用纯基于 setter 的注入进行依赖注入。
让我们有一个可以工作的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 −
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
| 2 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 3 | 在 com.tutorialspoint 包下创建 Java 类 |
| 4 | 在 src 文件夹下创建 Beans 配置文件 Beans.xml。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,然后运行应用程序,如下所述。 |
这是 TextEditor.java 文件的内容 −
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
// 用于注入依赖项的 setter 方法。
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// 一个返回 spellChecker 的 getter 方法
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
在这里您需要检查 setter 方法的命名约定。 为了设置一个变量 spellChecker,我们使用了与 Java POJO 类非常相似的 setSpellChecker() 方法。 让我们创建另一个依赖类文件 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> 标签进行基于构造函数的注入,使用 <property> 标签进行基于 setter 的注入。
第二个需要注意的重点是,如果你传递一个对象的引用,你需要使用 <property> 标签的 ref 属性,如果你是直接传递一个 value 那么你应该使用 value 属性。
完成创建源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,这将打印以下消息 −
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.
使用 p-namespace 的 XML 配置
如果你有很多 setter 方法,那么在 XML 配置文件中使用 p-namespace 会很方便。 让我们来看看区别 −
让我们考虑一个带有 <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>
可以使用 p-namespace 以更简洁的方式重写上述 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"
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-namespace 指定原始值和对象引用的区别。 -ref 部分表示这不是一个直接值,而是对另一个 bean 的引用。

