请教:有谁用过PropertyOverrideConfigurer这个类

这是Spring手册中关于该类的使用说明:
[quote]另一个bean工厂后置处理器PropertyOverrideConfigurer类似于PropertyPlaceholderConfigurer。但是与后者相比,前者对于bean属性可以有缺省值或者根本没有值。如果起覆盖作用的Properties文件没有某个bean属性的内容,那么将使用缺省的上下文定义。

bean工厂并不会意识到被覆盖,所以仅仅察看XML定义文件并不能立刻知道覆盖配置是否被使用了。在多个PropertyOverrideConfigurer实例中对一个bean属性定义了不同的值时,最后定义的值将被使用(由于覆盖机制)。

Properties文件的配置应该是如下的格式:

beanName.property=value
一个properties文件可能是下面这样的:

dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql:mydb
这个示例文件可用在这样一个bean容器:包含一个名为dataSource的bean,并且这个bean有driver和url属性。

注意它也支持组合的属性名称,只要路径中每个组件除了最后要被覆盖的属性外全都是非空的(比如通过构造器来初始化),在下例中:

foo.fred.bob.sammy=123
foo bean的fred属性的bob属性的sammy属性被设置为数值123。[/quote]
可是,我按着上面配置,Spring却报告错误,没有成功配置,下面把我的实验,简单介绍一下:
[code] <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>classpath:com/baobaotao/place/car.properties</value>
</list>
</property>
</bean>
<bean id="car" class="com.baobaotao.place.Car">
<property name="price" value="100"/>
<property name="maxSpeed" value="120"/>
</bean>[/code]
Car的类代码如下:
[code]package com.baobaotao.place;

public class Car {
private int maxSpeed;
public String brand;
private double price;
//get/setter
}[/code]
car.properties这个属性文件如下:
[code]car.maxSpeed=250
car.price=20000.00[/code]
Spring报出的错误如下:
[code]org.springframework.beans.factory.BeanInitializationException: Could not process key 'car.maxSpeed' in PropertyOverrideConfigurer; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'car' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:338)
at org.springframework.beans.factory.config.PropertyOverrideConfigurer.applyPropertyValue(PropertyOverrideConfigurer.java:139)
at org.springframework.beans.factory.config.PropertyOverrideConfigurer.processKey(PropertyOverrideConfigurer.java:127)
at org.springframework.beans.factory.config.PropertyOverrideConfigurer.processProperties(PropertyOverrideConfigurer.java:99)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:414)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:328)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at com.baobaotao.place.TestPropertyPlace.setUp(TestPropertyPlace.java:17)
...
[/code]
不知有没有使用过该类的朋友,请分享一下经验,谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值