Could not resolve placeholder 解決方案


用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屬性。
 
 
歸根到底就是規范沒到位,加載配置文件沒統一


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM