spring读取多个配置properties报错"Could not resolve placeholder"的解决方案

本文探讨了在Spring框架中遇到Couldnotresolveplaceholder错误的原因及解决方案,特别是在使用多个properties文件时。介绍了如何通过设置ignore-unresolvable属性或使用PropertyPlaceholderConfigurer来避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

  比如我有一个dao.xml读取dbConnect.properties,还有一个dfs.xml读取dfsManager.properties,然后web.xml统一load这两个xml文件

Xml代码   收藏代码
  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.                 WEB-INF/config/spring/dao.xml,   
  5.                 WEB-INF/config/spring/dfs.xml  
  6.         </param-value>  
  7. </context-param>  

如果这两个xml文件中分别有

Xml代码   收藏代码
  1. <!-- dao.xml -->  
  2. <context:property-placeholder location="WEB-INF/config/db/dbConnect.properties" />  
  3.   
  4. <!-- dfs.xml -->  
  5. <context:property-placeholder location="WEB-INF/config/dfs/dfsManager.properties" />  

那么,一定会出"Could not resolve placeholder"。

 

  一定要记住,不管是在一个spring文件还是在多个Spring文件被统一load的情况下,直接写

Xml代码   收藏代码
  1. <context:property-placeholder location="" />  
  2. <context:property-placeholder location="" />   

是不允许的。

 

解决方案:

  (1) 在Spring 3.0中,可以写:

Xml代码   收藏代码
  1. <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"  
  2. />  
  3. <context:property-placeholder location="yyy.properties" ignore-unresolvable="true"  
  4. />  

注意两个都要加上ignore-unresolvable="true",一个加另一个不加也是不行的

 

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

Java代码   收藏代码
  1. <bean id="随便" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="location" value="xxx.properties" />  
  3.     <property name="ignoreUnresolvablePlaceholders" value="true" />   
  4. </bean>  

  正因为如此,写多个PropertyPlaceholderConfigurer不加ignoreUnresolvablePlaceholders属性也是一样会出"Could not resolve placeholder"。

转载于:https://www.cnblogs.com/wonderlands/p/5552378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值