设值注入是Spring支持的多种依赖注入类型中的一种,也是最为常见的一种。设值(setter)注入指在通过调用无参构造器(或无参静态工厂方法,或工厂Bean的蜚静态工厂方法)实例化受管Bean后调用setter方法,从而建立起对象之间的依赖关系。
一、无参构造器实例化受管Bean
- public class HelloWorld implements IHelloWorld {
- protected static final Log log = LogFactory.getLog(HelloWorld.class);
- private IHelloStr helloStr;
- public HelloWorld() {
- super();
- }
- public void setHelloStr(IHelloStr str) {
- this.helloStr = str;
- }
- public String getContent() {
- return helloStr.getContent();
- }
- }
- <?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-2.5.xsd">
- <bean name="fileHello" class="test.FileHelloStrImpl">
- <constructor-arg>
- <value>helloworld.properties</value>
- </constructor-arg>
- </bean>
- <bean id="helloWorld" class="test.HelloWorld">
- <property name="helloStr" ref="fileHello" />
- </bean>
- </beans>
我们可以通过ref属性指定依赖的对象或value属性直接给出目标对象。
二、无参静态工厂方法实例化受管Bean
如果希望通过调用无参静态工厂方法获得天受管Bean,则需要往对应的POJO类中添加静态方法以,以下为实例:
- public class HelloWorld implements IHelloWorld {
- protected static final Log log = LogFactory.getLog(HelloWorld.class);
- private IHelloStr helloStr;
- public void setHelloStr(IHelloStr str) {
- this.helloStr = str;
- }
- public String getContent() {
- return helloStr.getContent();
- }
- public static HelloWorld getHelloWorld(){
- return new HelloWorld();
- }
- }
- <?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-2.5.xsd">
- <bean id="helloWorld" factory-method="getHelloWorld"
- class="test.HelloWorld">
- <property name="helloStr" ref="fileHello"/>
- </bean>
- <bean name="fileHello" class="test.FileHelloStrImpl">
- <constructor-arg>
- <value>helloworld.properties</value>
- </constructor-arg>
- </bean>
- </beans>
三、工厂Bean的非静态工厂方法实例受管Bean
同样的如果希望通过调用工厂Bean的非静态工厂方法获得受管Bean,则需要往对应的POJO类中添加非静态方法以,实例如下:
- public class HelloWorld implements IHelloWorld {
- protected static final Log log = LogFactory.getLog(HelloWorld.class);
- private IHelloStr helloStr;
- public void setHelloStr(IHelloStr str) {
- this.helloStr = str;
- }
- public String getContent() {
- return helloStr.getContent();
- }
- public HelloWorld makeHelloWorld(){
- return new HelloWorld();
- }
- }
- <?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-2.5.xsd">
- <bean id="helloWorldFactory" class="test.HelloWorld"/>
- <bean id="helloWorld" factory-bean="helloWorldFactory"
- factory-method="makeHelloWorld">
- <property name="helloStr" ref="fileHello"/>
- </bean>
- <bean name="fileHello"
- class="test.FileHelloStrImpl">
- <constructor-arg>
- <value>helloworld.properties</value>
- </constructor-arg>
- </bean>
- </beans>
配置的helloWorldFactory受管Bean扮演了工厂Bean的角色。