Could not resolve placeholder原因分析及解決方案
阿新 • • 發佈:2019-01-10
1. 問題描述
在啟動Junit跑單測載入資源配置檔案的時候遇到以下異常資訊:
Java程式碼
- Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'activity_template_id' in string value "${activity_template_id}"
- at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
- at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125
- at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258)
- at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282
- at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
- at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
- at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
- at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:206)
- ... 29 more
2. 問題分析
在讀取配置問題資訊的時候使用了入下方法:
Java程式碼
- "propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
- depends-on="genernatePropertyFile">
- "location">
- file:D:/idev/antx.properties
那麼出現異常資訊的可能性有三種
(1)location中的屬性檔案配置錯誤
(2)location中定義的配置檔案裡面沒有對應的placeholder值
(3)第三種就比較麻煩點,可能是Spring容器的配置問題
Spring容器採用反射掃描的發現機制,在探測到Spring容器中有一個org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就會停止對剩餘PropertyPlaceholderConfigurer的掃描(Spring 3.1已經使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
而這個基於名稱空間的配置,其實內部就是建立一個PropertyPlaceholderConfigurer Bean而已。換句話說,即Spring容器僅允許最多定義一個PropertyPlaceholderConfigurer(或),其餘的會被Spring忽略掉(其實Spring如果提供一個警告就好了)。
3.解決方案
(1)
Java程式碼- "propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
- depends-on="genernatePropertyFile">
- "location">
- file:D:/idev/antx.properties
- "ignoreUnresolvablePlaceholders" value="true
(2)
Java程式碼- "classpath*:redis.properties" ignore-unresolvable="true" />