Spring中使用PropertyPlaceholderConfigurer占位符得不到替换的问题

今天想把xml里面的一些值移到properties文件里方便修改,按照标准的做法,在xml文件的开头加上这么一段:


	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
		lazy-init="true">
		<property name="locations">
			<list>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>

但是情况有点特殊,我使用的是一个框架,它会调用Spring的类来加载xml文件。它首先加载a.xml文件,然后a.xml会引用b.xml文件,b.xml里面有一些占位符需要用到db.properties里面的value来替换。设置好以后重启,发现b.xml里面的占位符没有被替换的,怎么改都没用。


后来观察log发现这个框架是使用XmlBeanFactory来加载这些xml文件的,因为日志里有如下的一些信息:

(XmlBeanDefinitionReader.java:323) INFO  - Loading XML bean definitions from class path resource
因为按照平常自己的习惯,我会用ClassPathXmlApplicationContext来加载xml文件,难道这两者还有什么区别不成,查了一下资料,果然存在不同,ApplicationContext被推荐使用,因为它包含了很多BeanFactory没有的功能:

Table 3.5. Feature Matrix

FeatureBeanFactoryApplicationContext

Bean instantiation/wiring

Yes

Yes

Automatic BeanPostProcessor registration

No

Yes

Automatic BeanFactoryPostProcessor registration

No

Yes

Convenient MessageSource access (for i18n)

No

Yes

ApplicationEvent publication

No

Yes

详细的比较可以参考:Chapter 3. The IoC container  Section 3.8.1 
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory

而PropertyPlaceholderConfigurer是否能够发挥作用是和BeanFactoryPostProcessor 息息相关的,猜测问题在这,因为不能改框架的代码,后来把PropertyPlaceholderConfigurer的定义直接放到b.xml的开头,就能读到了。具体的原因说实话还没搞清楚,等有空的时候再看看吧。晕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值