用spring 配置加載properties文件的時候,報
Could not resolve placeholder 錯誤。
經過仔細查找,排除文件路徑,文件類容錯誤的原因,經過查找相關資料,出現"Could not resolve placeholder"很有可能是使用了多個PropertyPlaceholderConfigurer或者多個<context:property-placeholder>的原因或者是多個PropertyPlaceholderConfigurer與<context:property-placeholder>混合使用。
如果配置如下一定會報以上錯誤,不管是在多個配置文件中還是分別在不同文件中
- <context:property-placeholder location="WEB-INF/config/db/XX.properties" />
- <context:property-placeholder location="WEB-INF/config/dfs/XX.properties" />
解決方案:
在Spring3中可以用如下方式解決,增加ignore-unresolvable="true"屬性,注意必須都要加上
在Spring3中可以用如下方式解決,增加ignore-unresolvable="true"屬性,注意必須都要加上
- <context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />
- <context:property-placeholder location="yyy.properties" ignore-unresolvable="true"
- />
在Spring 2.5中,<context:property-placeholder>沒有ignore-unresolvable屬性,此時可以改用PropertyPlaceholderConfigurer。其實<context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />與下面的配置是等價的
- <bean id="XX" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="xxx.properties" />
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- </bean>
系統中如果報如上錯誤,可以這樣查找:搜索系統中所有包含property-placeholder的文件,看是否包含ignore-unresolvable屬性,然后搜索系統中所有包含PropertyPlaceholderConfigurer文件,看是否包含ignoreUnresolvablePlaceholders屬性。
今天系統中報如上錯誤,就是因為按照PropertyPlaceholderConfigurer方式沒有配置ignoreUnresolvablePlaceholders屬性,而按照context:property-placeholder方式配置的都包含了ignore-unresolvable屬性。
歸根到底就是規范沒到位,加載配置文件沒統一