Spring高级注入之属性值注入

实际应用中,某个实例的属性可能是另一个对象的一个属性,Spring支持将bean实例的属性值直接赋值给一个变量

属性值的注入,是通过PropertyPathFactoryBean完成的,PropertyPathFactoryBean用来获取目标bean的属性,获得的值可以注入到其他bean,也可以定义成新的bean

实体类:

 

Java代码    收藏代码
  1. package Bean.superIOCparam;  
  2.   
  3. public class Person {  
  4.    private Son son;  
  5.    private String age;  
  6. public String getAge() {  
  7.     return age;  
  8. }  
  9. public void setAge(String age) {  
  10.     this.age = age;  
  11. }  
  12. public Son getSon() {  
  13.     return son;  
  14. }  
  15. public void setSon(Son son) {  
  16.     this.son = son;  
  17. }  
  18. }  
  19.   
  20.   
  21. package Bean.superIOCparam;  
  22.   
  23. public class Son {  
  24.   private String age;  
  25.   
  26. public String getAge() {  
  27.     return age;  
  28. }  
  29.   
  30. public void setAge(String age) {  
  31.     this.age = age;  
  32. }  
  33. }  

 配置文件:提供四种注入

Xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3.   
  4. <beans>  
  5.   
  6.   <bean id="person" class="Bean.superIOCparam.Person" singleton="false">  
  7.      <property name="age">  
  8.         <value>30</value>  
  9.      </property>  
  10.      <property name="son">  
  11.         <bean class="Bean.superIOCparam.Son">  
  12.            <property name="age">  
  13.               <value>16</value>  
  14.            </property>  
  15.         </bean>  
  16.      </property>  
  17.   </bean>  
  18.     
  19.   <!--如下将会将person的属性son的属性age传入son1实例的age属性-->  
  20.     <bean id="son1" class="Bean.superIOCparam.Son">  
  21.         <property name="age">  
  22.           <!--以下是访问bean属性的简单方式,这样可以将person这个bean的age属性赋值给son1这个bean的age属性-->             
  23.          <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>  
  24.         </property>  
  25.     </bean>  
  26.       
  27.     <!-- 以下将会获得结果son,它将是person bean的son的数值-->  
  28.     <bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">  
  29.        <property name="targetBeanName">  
  30.          <value>person</value>  
  31.        </property>  
  32.        <property name="propertyPath">  
  33.          <value>son</value>  
  34.        </property>  
  35.     </bean>  
  36.       
  37.      <!-- 以下将会获得结果16,它将是person bean的son的age属性-->  
  38.     <bean id="son3" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">  
  39.        <property name="targetBeanName">  
  40.          <value>person</value>  
  41.        </property>  
  42.        <property name="propertyPath">  
  43.          <value>son.age</value>  
  44.        </property>  
  45.     </bean>  
  46.       
  47.     <!-- 以下会获得结果为30 ,它将是获得该bean的内部bean的age属性-->  
  48.     <bean id="son4" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">  
  49.         <property name="targetObject">  
  50.             <bean class="Bean.superIOCparam.Person">  
  51.                 <property name="age"><value>30</value></property>  
  52.             </bean>  
  53.         </property>  
  54.         <property name="propertyPath"><value>age</value></property>  
  55.     </bean>  
  56. </beans>  

 测试代码:

Java代码    收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.           
  3.         String path=new Test().getClass().getResource("/").getPath();  
  4.         String realpath=path.substring(1, path.length());  
  5.         ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/superIOCparam.xml");  
  6.         Son son1=(Son)context.getBean("son1");  
  7.         Son son2=(Son)context.getBean("son2");  
  8.   
  9.         System.out.println("person age is:"+son1.getAge());  
  10.         System.out.println("person age is:"+son2.getAge());  
  11.         System.out.println(context.getBean("son3"));  
  12.         System.out.println(context.getBean("son4"));  
  13.     }  

 

运行结果:

person age is:16
person age is:16
16
30

猜你喜欢

转载自xiaoqis.iteye.com/blog/1934036