Spring受管Bean依赖注入(设值注入)

设值注入是Spring支持的多种依赖注入类型中的一种,也是最为常见的一种。设值(setter)注入指在通过调用无参构造器(或无参静态工厂方法,或工厂Bean的蜚静态工厂方法)实例化受管Bean后调用setter方法,从而建立起对象之间的依赖关系。

一、无参构造器实例化受管Bean

[java]  view plain copy
  1. public class HelloWorld implements IHelloWorld {  
  2.       
  3.     protected static final Log log = LogFactory.getLog(HelloWorld.class);  
  4.     private IHelloStr helloStr;  
  5.       
  6.     public HelloWorld() {  
  7.         super();  
  8.     }  
  9.     public void setHelloStr(IHelloStr str) {  
  10.         this.helloStr = str;  
  11.     }  
  12.     public String getContent() {  
  13.         return helloStr.getContent();  
  14.     }  
  15. }  

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.     <bean name="fileHello" class="test.FileHelloStrImpl">  
  7.         <constructor-arg>  
  8.             <value>helloworld.properties</value>  
  9.         </constructor-arg>  
  10.     </bean>  
  11.     <bean id="helloWorld" class="test.HelloWorld">  
  12.         <property name="helloStr" ref="fileHello" />  
  13.     </bean>  
  14. </beans>  

我们可以通过ref属性指定依赖的对象或value属性直接给出目标对象。

二、无参静态工厂方法实例化受管Bean

如果希望通过调用无参静态工厂方法获得天受管Bean,则需要往对应的POJO类中添加静态方法以,以下为实例:

[java]  view plain copy
  1. public class HelloWorld implements IHelloWorld {  
  2.       
  3.     protected static final Log log = LogFactory.getLog(HelloWorld.class);  
  4.     private IHelloStr helloStr;  
  5.     public void setHelloStr(IHelloStr str) {  
  6.         this.helloStr = str;  
  7.     }  
  8.     public String getContent() {  
  9.         return helloStr.getContent();  
  10.     }  
  11.       
  12.     public static HelloWorld getHelloWorld(){  
  13.         return new HelloWorld();  
  14.     }  
  15. }  

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.      <bean id="helloWorld"  factory-method="getHelloWorld"   
  7.           class="test.HelloWorld">  
  8.           <property name="helloStr" ref="fileHello"/>  
  9.      </bean>  
  10.        
  11.      <bean name="fileHello" class="test.FileHelloStrImpl">  
  12.           <constructor-arg>  
  13.                <value>helloworld.properties</value>  
  14.           </constructor-arg>    
  15.      </bean>  
  16.        
  17. </beans>  

三、工厂Bean的非静态工厂方法实例受管Bean

同样的如果希望通过调用工厂Bean的非静态工厂方法获得受管Bean,则需要往对应的POJO类中添加非静态方法以,实例如下:

[java]  view plain copy
  1. public class HelloWorld implements IHelloWorld {  
  2.       
  3.     protected static final Log log = LogFactory.getLog(HelloWorld.class);  
  4.     private IHelloStr helloStr;  
  5.     public void setHelloStr(IHelloStr str) {  
  6.         this.helloStr = str;  
  7.     }  
  8.     public String getContent() {  
  9.         return helloStr.getContent();  
  10.     }  
  11.           
  12.     public HelloWorld makeHelloWorld(){  
  13.         return new HelloWorld();  
  14.     }  
  15. }  

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.      <bean id="helloWorldFactory" class="test.HelloWorld"/>  
  7.        
  8.      <bean id="helloWorld" factory-bean="helloWorldFactory"  
  9.             factory-method="makeHelloWorld">  
  10.           <property name="helloStr" ref="fileHello"/>  
  11.      </bean>  
  12.        
  13.      <bean name="fileHello"   
  14.           class="test.FileHelloStrImpl">  
  15.           <constructor-arg>  
  16.                <value>helloworld.properties</value>  
  17.           </constructor-arg>    
  18.      </bean>  
  19.        
  20. </beans>  

配置的helloWorldFactory受管Bean扮演了工厂Bean的角色。


转载自:http://blog.csdn.net/zhangxs_3/article/details/4345575

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值