Spring使用PropertyPathFactoryBean進行屬性值注入

來源:互聯網
上載者:User

本文轉自:http://www.blogjava.net/wangxinsh55/archive/2010/08/10/328427.html

實際應用中,某個執行個體的屬性可能是另一個對象的一個屬性,Spring支援將bean執行個體的屬性值直接賦值給一個變數

屬性值的注入,是通過PropertyPathFactoryBean完成的,PropertyPathFactoryBean用來擷取目標bean的屬性,獲得的值可以注入到其他bean,也可以定義成新的bean

實體類:

public class Person {    private Son son;    private String age;        public Son getSon() {        return son;    }        public void setSon(Son son) {        this.son = son;    }        public String getAge() {        return age;    }        public void setAge(String age) {        this.age = age;    }}
public class Son {    private String age;    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }}

設定檔:提供四種注入

<?xml version="1.0" encoding="GB2312"?><beans default-autowire="byName"xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd        http://www.springframework.org/schema/util http://localhost:8080/schema/www.springframework.org/schema/util/spring-util-2.0.xsd "> <bean id="person" class="com.alibaba.chj.factorybean.Person" scope="prototype">     <property name="age">        <value>30</value>     </property>     <property name="son">        <bean class="com.alibaba.chj.factorybean.Son">           <property name="age">              <value>16</value>           </property>        </bean>     </property>  </bean>    <!--如下將會將person的屬性son的屬性age傳入son1執行個體的age屬性-->    <bean id="son1" class="com.alibaba.chj.factorybean.Son">        <property name="age">          <!--以下是訪問bean屬性的簡單方式,這樣可以將person這個bean的age屬性賦值給son1這個bean的age屬性-->                    <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>        </property>    </bean>        <!-- 以下將會獲得結果son,它將是person bean的son的數值-->    <bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">       <property name="targetBeanName">         <value>person</value>       </property>       <property name="propertyPath">         <value>son</value>       </property>    </bean>         <!-- 以下將會獲得結果16,它將是person bean的son的age屬性-->    <bean id="son3" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">       <property name="targetBeanName">         <value>person</value>       </property>       <property name="propertyPath">         <value>son.age</value>       </property>    </bean>        <!-- 以下會獲得結果為30 ,它將是獲得該bean的內部bean的age屬性-->    <bean id="son4" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">        <property name="targetObject">            <bean class="com.alibaba.chj.factorybean.Person">                <property name="age"><value>30</value></property>            </bean>        </property>        <property name="propertyPath"><value>age</value></property>    </bean></beans>

測試代碼:

public class PropertyPathTest {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext(                                                                        "com/alibaba/chj/factorybean/propertyPathFactoryBean.xml");        Son son1 = (Son) context.getBean("son1");        Son son2 = (Son) context.getBean("son2");        System.out.println("person age is:" + son1.getAge());        System.out.println("person age is:" + son2.getAge());        System.out.println(context.getBean("son3"));        System.out.println(context.getBean("son4"));    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.