Spring受管Bean依賴注入(設值注入)

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.