实际应用中,某个实例的属性可能是另一个对象的一个属性,Spring支持将bean实例的属性值直接赋值给一个变量
属性值的注入,是通过PropertyPathFactoryBean完成的,PropertyPathFactoryBean用来获取目标bean的属性,获得的值可以注入到其他bean,也可以定义成新的bean
实体类:
packageBean.superIOCparam;

publicclassPerson...{
privateSonson;
privateStringage;
publicStringgetAge()...{
returnage;
}
publicvoidsetAge(Stringage)...{
this.age=age;
}
publicSongetSon()...{
returnson;
}
publicvoidsetSon(Sonson)...{
this.son=son;
}
}

packageBean.superIOCparam;

publicclassSon...{
privateStringage;

publicStringgetAge()...{
returnage;
}

publicvoidsetAge(Stringage)...{
this.age=age;
}
}
配置文件:提供四种注入
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="person"class="Bean.superIOCparam.Person"singleton="false">
<propertyname="age">
<value>30</value>
</property>
<propertyname="son">
<beanclass="Bean.superIOCparam.Son">
<propertyname="age">
<value>16</value>
</property>
</bean>
</property>
</bean>
<!--如下将会将person的属性son的属性age传入son1实例的age属性-->
<beanid="son1"class="Bean.superIOCparam.Son">
<propertyname="age">
<!--以下是访问bean属性的简单方式,这样可以将person这个bean的age属性赋值给son1这个bean的age属性-->
<beanid="person.son.age"class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean>

<!--以下将会获得结果son,它将是personbean的son的数值-->
<beanid="son2"class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<propertyname="targetBeanName">
<value>person</value>
</property>
<propertyname="propertyPath">
<value>son</value>
</property>
</bean>

<!--以下将会获得结果16,它将是personbean的son的age属性-->
<beanid="son3"class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<propertyname="targetBeanName">
<value>person</value>
</property>
<propertyname="propertyPath">
<value>son.age</value>
</property>
</bean>

<!--以下会获得结果为30,它将是获得该bean的内部bean的age属性-->
<beanid="son4"class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<propertyname="targetObject">
<beanclass="Bean.superIOCparam.Person">
<propertyname="age"><value>30</value></property>
</bean>
</property>
<propertyname="propertyPath"><value>age</value></property>
</bean>
</beans>
</property>
</bean>
<!--以下将会获得结果son,它将是personbean的son的数值-->
<beanid="son2"class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<propertyname="targetBeanName">
<value>person</value>
</property>
<propertyname="propertyPath">
<value>son</value>
</property>
</bean>
<!--以下将会获得结果16,它将是personbean的son的age属性-->
<beanid="son3"class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<propertyname="targetBeanName">
<value>person</value>
</property>
<propertyname="propertyPath">
<value>son.age</value>
</property>
</bean>
<!--以下会获得结果为30,它将是获得该bean的内部bean的age属性-->
<beanid="son4"class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<propertyname="targetObject">
<beanclass="Bean.superIOCparam.Person">
<propertyname="age"><value>30</value></property>
</bean>
</property>
<propertyname="propertyPath"><value>age</value></property>
</bean>
</beans>
测试代码:

publicstaticvoidmain(String[]args)throwsException...{
Stringpath=newTest().getClass().getResource("/").getPath();
Stringrealpath=path.substring(1,path.length());
ApplicationContextcontext=newFileSystemXmlApplicationContext(realpath+"/superIOCparam.xml");
Sonson1=(Son)context.getBean("son1");
Sonson2=(Son)context.getBean("son2");
System.out.println("personageis:"+son1.getAge());
System.out.println("personageis:"+son2.getAge());
System.out.println(context.getBean("son3"));
System.out.println(context.getBean("son4"));
}
运行结果:
person age is:16
person age is:16
16
30
本文介绍Spring框架中如何使用PropertyPathFactoryBean进行属性注入,通过不同配置实现bean属性间的传递。展示了四种注入方式,并提供了测试代码及运行结果。
1万+

被折叠的 条评论
为什么被折叠?



