設值注入是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的角色。