Spring笔记:根据对象属性声明Bean
描述:在Spring IoC容器中,可能希望根据对象属性来声明Bean。比如:ProductRanking类中存在属性Product,那么在配置文件中声明ProductRanking时,对于内部属性Product很可能是采用内部Bean的方式来声明的。此时可以利用Spring内置的工厂BeanPropertyPathFactoryBean获取它了.
例子:
在ProductRanking类:
package chapter3_7; public class ProductRanking { private Produce producte; public Produce getProducte() { return producte; } public void setProducte(Produce producte) { this.producte = producte; } }
在配置文件中,使用工厂BeanPropertyFactoryBean根据对象属性声明Bean.
<bean id="productRanking" class="chapter3_7.ProductRanking"> <property name="produce"> <bean class="chapter3_7.Disc"> <constructor-arg value="Disc"></constructor-arg> <constructor-arg value="22"></constructor-arg> </bean> </property> </bean> <bean id="produce" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetObject" value="productRanking"></property> <property name="propertyPath" value="produce"></property> </bean>
<!--或者:-->
<util:property-path id="produce" path="productRanking.produce"/>

浙公网安备 33010602011771号