当容器调用一个无参构造函数或者一个无参的静态工厂方法初始化你的bean后,容器通过在你的bean上调用setter的方法完成依赖注入的。
Example
SpellChecker.java
package com.soygrow.SetterDependency;
public class SpellChecker {
public SpellChecker() {
System.out.println("Inside SpellChecker constructor ...");
}
public void checkSpelling() {
System.out.println("Inside checkSpelling ...");
}
}
TextEditor.java
package com.soygrow.SetterDependency;
public class TextEditor {
private SpellChecker spellChecker;
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker ...");
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
MainApp.java
package com.soygrow.SetterDependency;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SetterBeans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
SetterBeans.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.xsd">
<bean id="textEditor" class="com.soygrow.SetterDependency.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>
<bean id="spellChecker" class="com.soygrow.SetterDependency.SpellChecker">
</bean>
</beans>
如果一切正常,那么运行的结果:
Inside SpellChecker constructor ...
Inside setSpellChecker ...
Inside checkSpelling ...
区别
构造注入以及setter方法的注入有几个点需要注意:
- 构造注入中bean的内部使用 <constructor-arg>
- setter方法注入在bean定义的参数使用<property>
- 如果bean中参数需要另外一个bean,那么使用<property>
的ref属性,如果你需要传递值,可以使用value属性,这个在两种方式中是一致的。
XML Configuration using p-namespace
如果你存在很多的setter方法,那么可以使用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"
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-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>
上面只要区分出值和引用的两种参数的传递方式:
- 引用其他bean需要再后面添加-ref
Spring教程专栏地址:http://blog.youkuaiyun.com/column/details/19452.html