除去properties文件路径错误、拼写错误外,出现”Could not resolve placeholder”很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>的原因。 一定要记住,不管是在一个Spring文件还是在多个Spring文件被统一load的情况下,直接写:
<context:property-placeholder location=""/>
<context:property-placeholder location=""/>
是错误的!
解决方案
在Spring 3.0中,可以写:
<context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:/redis.properties" ignore-unresolvable="true"/>
注意两个xml配置文件都要加上ignore-unresolvable=”true”,一个加另一个不加也是不行的。
在Spring 2.5中,没有ignore-unresolvable属性,此时可以改用PropertyPlaceholderConfigurer。其实与下面的配置是等价的
<bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath*:/redis.properties</value>
</list>
</property>
</bean>
正因为如此,写多个PropertyPlaceholderConfigurer不加ignoreUnresolvablePlaceholders属性也是一样会出”Could not resolve placeholder”。
原文: https://blog.csdn.net/chl191623691/article/details/80108533