I. knowledge points
If you want to declare beans from an object attribute or nested attribute (that is, the attribute path) in the Spring IoC container, you can use the built-in factory bean propertypathfactorybean or <util: property-path> label.
Ii. Sample Code
Create a productranking class
package com.jackie.codeproject.springrecipesnote.springadvancedioc;/** * @author huangfeifei * */public class ProductRanking {private Product bestSeller;public Product getBestSeller() {return bestSeller;}public void setBestSeller(Product bestSeller) {this.bestSeller = bestSeller;}}
In the following bean declaration, the bestseller attribute is declared by an internal bean. According to the definition, internal beans cannot be read by name. But it can be read as the property of the productranking bean. Factory bean propertypathfactorybean can be used to declare bean from object attributes or attribute paths.
<bean id="productRanking" class="com.jackie.codeproject.springrecipesnote.springadvancedioc.ProductRanking"> <property name="bestSeller"> <bean class="com.jackie.codeproject.springrecipesnote.springadvancedioc.Disc"> <property name="name" value="CD-RW" /> <property name="price" value="1.5" /> </bean> </property> </bean> <bean id="bestSeller" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <property name="targetObject" ref="productRanking" /> <property name="propertyPath" value="bestSeller" /></bean>
Note: The propertypath attribute of propertypathfactorybean can accept not only a single attribute name, but also an attribute path separated by periods.The preceding bean configuration is equivalent to the following code:
Product bestseller = productranking. getbestseller ();
In addition to explicitly specifying the targetobject and propertypath attributes, you can combine them into the names of propertypathfactorybean. The disadvantage is that the bean name is too long.
<bean id="productRanking" class="com.jackie.codeproject.springrecipesnote.springadvancedioc.ProductRanking"> <property name="bestSeller"> <bean class="com.jackie.codeproject.springrecipesnote.springadvancedioc.Disc"> <property name="name" value="CD-RW" /> <property name="price" value="1.5" /> </bean> </property> </bean><bean id="productRanking.bestSeller" class="org.springframework.beans.factory.config.PropertyPathFactoryBean" />
Spring2.x<Util: Property-path> labelDeclare bean from an object property or attribute path. This declaration method is simpler than propertypathfactorybean.
<?xml version="1.0" encoding="UTF-8"?><beans 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-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="productRanking" class="com.jackie.codeproject.springrecipesnote.springadvancedioc.ProductRanking"> <property name="bestSeller"> <bean class="com.jackie.codeproject.springrecipesnote.springadvancedioc.Disc"> <property name="name" value="CD-RW" /> <property name="price" value="1.5" /> </bean> </property> </bean> <util:property-path id="bestSeller" path="productRanking.bestSeller" /></beans>