spring讀取多個配置properties報錯"Could not resolve placeholder"的解決方案


除去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"。


免責聲明!

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



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