spring报“Could not resolve placeholder”错误,解决方案

用spring 配置加载properties文件的时候,报
Could not resolve placeholder 错误。
经过仔细查找,排除文件路径,文件类容错误的原因,经过查找相关资料, 出现"Could notresolve placeholder"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>的原因或者是多个 PropertyPlaceholderConfigurer与 <context:property-placeholder>混合使用。
如果配置如下一定会报以上错误,不管是在多个配置文件中还是分别在不同文件中

  1. <context:property-placeholder location="WEB-INF/config/db/XX.properties" />   
  2. <context:property-placeholder location="WEB-INF/config/dfs/XX.properties" />  


解决方案:
在Spring3中可以用如下方式解决,增加 ignore-unresolvable = "true"属性,注意必须都要加上

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

在Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,此时可以改用PropertyPlaceholderConfigurer。其实<context:property-placeholderlocation="xxx.properties"  ignore-unresolvable="true"  />与下面的配置是等价的

  1. <bean id="XX" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="location" value="xxx.properties" />  
  3.     <property name="ignoreUnresolvablePlaceholders" value="true" />   
  4. </bean>  


系统中如果报如上错误,可以这样查找:搜索系统中所有包含 property-placeholder文件,看是否包含 ignore-unresolvable属性,然后搜索系统中所有包含 PropertyPlaceholderConfigurer文件,看是否包含 ignoreUnresolvablePlaceholders属性。

今天系统中报如上错误,就是因为按照 PropertyPlaceholderConfigurer方式没有配置 ignoreUnresolvablePlaceholders属性,而按照 context:property-placeholder方式配置的都包含了 ignore-unresolvable属性。


归根到底就是规范没到位,加载配置文件没统一
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值