設值注入是Spring支援的多種依賴注入類型中的一種,也是最為常見的一種。設值(setter)注入指在通過調用無參構造器(或無參靜態Factory 方法,或工廠Bean的蜚靜態Factory 方法)執行個體化受管Bean後調用setter方法,從而建立起對象之間的依賴關係。
一、無參構造器執行個體化受管Bean
public class HelloWorld implements IHelloWorld {</p><p> protected static final Log log = LogFactory.getLog(HelloWorld.class);<br /> private IHelloStr helloStr;</p><p> public HelloWorld() {<br />super();<br />}<br />public void setHelloStr(IHelloStr str) {<br />this.helloStr = str;<br />}<br /> public String getContent() {<br /> return helloStr.getContent();<br /> }<br />}
<?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://www.springframework.org/schema/beans<br /> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><br /><bean name="fileHello" class="test.FileHelloStrImpl"><br /><constructor-arg><br /><value>helloworld.properties</value><br /></constructor-arg><br /></bean><br /><bean id="helloWorld" class="test.HelloWorld"><br /><property name="helloStr" ref="fileHello" /><br /></bean><br /></beans>
我們可以通過ref屬性指定依賴的對象或value屬性直接給出目標對象。
二、無參靜態Factory 方法執行個體化受管Bean
如果希望通過調用無參靜態Factory 方法獲得天受管Bean,則需要往對應的POJO類中添加靜態方法以,以下為執行個體:
public class HelloWorld implements IHelloWorld {</p><p> protected static final Log log = LogFactory.getLog(HelloWorld.class);<br /> private IHelloStr helloStr;<br />public void setHelloStr(IHelloStr str) {<br />this.helloStr = str;<br />}<br /> public String getContent() {<br /> return helloStr.getContent();<br /> }</p><p> public static HelloWorld getHelloWorld(){<br /> return new HelloWorld();<br /> }<br />}
<?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://www.springframework.org/schema/beans<br />http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><br /> <bean id="helloWorld" factory-method="getHelloWorld"<br /> class="test.HelloWorld"><br /> <property name="helloStr" ref="fileHello"/><br /> </bean></p><p> <bean name="fileHello" class="test.FileHelloStrImpl"><br /> <constructor-arg><br /> <value>helloworld.properties</value><br /> </constructor-arg><br /> </bean></p><p></beans>
三、工廠Bean的非靜態Factory 方法執行個體受管Bean
同樣的如果希望通過調用工廠Bean的非靜態Factory 方法獲得受管Bean,則需要往對應的POJO類中添加非靜態方法以,執行個體如下:
public class HelloWorld implements IHelloWorld {</p><p> protected static final Log log = LogFactory.getLog(HelloWorld.class);<br /> private IHelloStr helloStr;<br />public void setHelloStr(IHelloStr str) {<br />this.helloStr = str;<br />}<br /> public String getContent() {<br /> return helloStr.getContent();<br /> }</p><p> public HelloWorld makeHelloWorld(){<br /> return new HelloWorld();<br /> }<br />}
<?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://www.springframework.org/schema/beans<br />http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><br /> <bean id="helloWorldFactory" class="test.HelloWorld"/></p><p> <bean id="helloWorld" factory-bean="helloWorldFactory"<br /> factory-method="makeHelloWorld"><br /> <property name="helloStr" ref="fileHello"/><br /> </bean></p><p> <bean name="fileHello"<br /> class="test.FileHelloStrImpl"><br /> <constructor-arg><br /> <value>helloworld.properties</value><br /> </constructor-arg><br /> </bean></p><p></beans>
配置的helloWorldFactory受管Bean扮演了工廠Bean的角色。