Could not resolve placeholder 解決方案

轉:http://blog.163.com/wf_shunqiziran/blog/static/176307209201282755010505/

 
 
用spring 配置加載properties文件的時候,報
Could not resolve placeholder 錯誤。
經過仔細查找,排除文件路徑,文件類容錯誤的原因,經過查找相關資料,出現"Could not resolve 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-placeholder location="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屬性。
歸根到底就是規範沒到位,加載配置文件沒統一
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章