Spring strategy learning notes (2.04) -- declare bean from Object Attributes

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.