本文轉自: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")); }}