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

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

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

[java] view plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章