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

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