Spring入门学习——从对象属性中声明Bean

来源:互联网 发布:我的世界透视矿物js 编辑:程序博客网 时间:2024/05/20 04:51
现在打算从一个对象属性或者嵌套的属性(也就是属性路径)中声明Spring IoC容器中的一个Bean
Spring提供了内建的工厂Bean  PropertyPathFactoryBean和<util:property-path>标记来完成从对象属性声明一个Bean。

应用场景:在之前的商品折扣代码基础上,增加一个ProductRanking类,来获取最好销量的商品。

package com.cgy.springrecipes.shop;

public class ProductRanking {

private Product bestSeller;

public void setBestSeller(Product bestSeller) {
this.bestSeller = bestSeller;
}

public Product getBestSeller() {
return bestSeller;
}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="productRanking"
class="com.cgy.springrecipes.shop.ProductRanking">
<property name="bestSeller">
<bean class="com.cgy.springrecipes.shop.Disc">
<property name="name" value="taylor"/>
<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>

</beans>


可能从对象属性中声明Bean在字面上难以明白,但是配合xml代码就不难理解,首先在我们productRanking这个<bean>里面
配置了一个属性bestSeller,这个没什么特别,但是这个属性使用的是内部<bean>写法,不允许我们根据名称读取内部bean;但是通过对象属性这个思路,我们可以将作为productRanking bean的属性来读取,这就是对象属性在这里代码的体现( 尽管现在看上去这个特性用途不大)。

需要注意的是,PropertyPathFactoryBean的一般使用需要指定targetObject属性propertyPath属性

而Main的代码只要进行一点修改就可以,实质没有变化,代码如下:

package com.cgy.springrecipes.shop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_two.xml");
//Product kingston = (Product) context.getBean("battery");
//Product taylor = (Product) context.getBean("disc");
//System.out.println(kingston);
//System.out.println(taylor);
Product bestSeller = (Product) context.getBean("bestSeller");
System.out.println(bestSeller);
}
}


****************************************************************************************************************************************
和从静态字段声明Bean一样,从对象属性中声明Bean也有一个简写配置,那就是使用<util:property-path>
配置文件代码修改如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="productRanking"
class="com.cgy.springrecipes.shop.ProductRanking">
<property name="bestSeller">
<bean class="com.cgy.springrecipes.shop.Disc">
<property name="name" value="taylor"/>
<property name="price" value="1.5"/>
</bean>
</property>
</bean>

<util:property-path id="bestSeller" path="productRanking.bestSeller"/>

</beans>

使用<util:property-path>标记需要指定一个id(就是bean的id),还有一个path属性,写法正如开头说的一样,对象属性,但是这个与对象属性写法有点差别,因为是 BeanId.property,写法上类似 对象.属性。
0 0
原创粉丝点击