个人认为以下方法只是忽略了错误,但是并没有解决错误,web项目并不会因为忽略该错误而成功加载property文件。
该错误出现的原因好像是同时加载多个properties文件(无论是在同一个配置文件中还是多个配置文件中)
原地址: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>混合使用。
如果配置如下一定会报以上错误,不管是在多个配置文件中还是分别在不同文件中
- <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"
- />
- <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属性。
这样修改:
<property name="locations" value="classpath*:*.properties"/>
这样修改:
<property name="locations" value="classpath*:*.properties"/>
归根到底就是规范没到位,加载配置文件没统一