Spring中报"Could not resolve placeholder"的解决方案

除去properites文件路径错误、拼写错误外,出现”Could not resolve placeholder”很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>(<context:property-placeholder>底层也是PropertyPlaceholderConfigurer)的原因。


因为spring扫描注册PropertyPlaceholderConfigurer的bean被设置为单例模式,spring只会扫描一个PropertyPlaceholderConfigurer或者<context:property-placeholder>配置,其它多余将被忽略,所以才会出现替代属性值的占位符无法解析。

解决方案:

方案一:

坚持使用多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>,那么需要添加ignoreUnresolvablePlaceholders属性或者ignore-unresolvable属性,设置其值为true

如下代码所示:

<bean id="xxx" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="xxx.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="yyy" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="yyy.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

或者:

<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>  
<context:property-placeholder location="yyy.properties" ignore-unresolvable="true"/>  

参照:http://blog.csdn.net/linwei_1029/article/details/6873764

方案二:

将多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>合并到一个里面,统一加载所有的资源文件

如下代码所示:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:xxx.properties</value>
            <value>classpath:jyyy.properties</value>
        </list>
    </property>
</bean> 

或者:

<context:property-placeholder location="classpath:xxx.properties,classpath:yyy.properties"/>

加载的日志可以发现,创建的propertyPlaceholderConfigurer这个bean是一个单例bean 
这里写图片描述

参照:http://stamen.iteye.com/blog/1926166

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值