现在的位置: 首页 > 综合 > 正文

Spring攻略学习笔记(2.04)——从对象属性中声明Bean

2013年09月20日 ⁄ 综合 ⁄ 共 2654字 ⁄ 字号 评论关闭

    一、知识点     

          如果你打算从一个对象属性或者嵌套属性(即属性路径)在Spring IoC容器中声明Bean,可以使用内置的工厂Bean PropertyPathFactoryBean或者Spring2.x中的<util:property-path> 标签。

   二、代码示例

          创建一个ProductRanking类

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;
	}
}

         在下面的Bean声明中,bestSeller属性由一个内部Bean声明。根据定义,不能用名称读取内部Bean。但可以将其作为productRanking这个bean的属性来读取。工厂Bean PropertyPathFactoryBean可以用来从对象属性或者属性路径声明Bean。

<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>

       注意,PropertyPathFactoryBean的propertyPath属性不仅可以接受单个属性名称,也可以接受以句点为分隔符的属性路径。前述的Bean配置与如下代码等价:

        Product bestSeller = productRanking.getBestSeller();

        除了显示地指定targetObject和propertyPath属性之外,可以将它们合并为PropertyPathFactoryBean的名称,缺点是Bean的名称太长。

<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>标签从一个对象属性或者属性路径中声明Bean。与使用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>

抱歉!评论已关闭.