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

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

 

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

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文件中分别有spa

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"。xml

 

  必定要记住,无论是在一个Spring文件仍是在多个Spring文件被统一load的状况下,直接写blog

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

是不容许的。get

 

解决方案:it

  (1) 在Spring 3.0中,能够写:io

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",一个加另外一个不加也是不行的class

 

  (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"。

 

  虽然二者是的等价的,但估计你们仍是喜欢写<context:property-placeholder>多一些,毕竟简单一些嘛。因此若是是Spring 3.0,直接用解决方案(1)再简单不过了;若是是Spring 2.5,须要费点力气改写成PropertyPlaceholderConfigurer

相关文章
相关标签/搜索