浏览 6061 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-01-06
引用 另一个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。 可是,我按着上面配置,Spring却报告错误,没有成功配置,下面把我的实验,简单介绍一下: <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> Car的类代码如下: package com.baobaotao.place; public class Car { private int maxSpeed; public String brand; private double price; //get/setter } car.properties这个属性文件如下: car.maxSpeed=250 car.price=20000.00 Spring报出的错误如下: 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) ... 不知有没有使用过该类的朋友,请分享一下经验,谢谢! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-01-06
一直用PropertyPlaceholderConfigurer,虽然知道PropertyOverrideConfigurer,但从来没有用过...
|
|
返回顶楼 | |
发表时间:2007-01-10
我按照Spring的文档总是实验不成功,还没有时间去深入研究源码,我想不应该会有源码上的问题。
|
|
返回顶楼 | |
发表时间:2007-01-11
用过,就是照着spring手册上用的,看你的配置也没什么错呀.
|
|
返回顶楼 | |
发表时间:2007-01-11
效率问题,还是placeHolder要方便!
|
|
返回顶楼 | |
发表时间:2007-01-11
直接就用你的代码,完全没有问题啊,有拼写错误吧?
|
|
返回顶楼 | |
发表时间:2007-01-11
我也碰到此问题,是在写测试时碰到的,关注中...
|
|
返回顶楼 | |
发表时间:2007-01-15
引用 直接就用你的代码,完全没有问题啊,有拼写错误吧?
我检查了,自己写的好象并没有错啊。能不能把你程序的打个包给我?谢谢! |
|
返回顶楼 | |