springmvc 中@Value("${KEY}") 出现 $Value{}ntException: Could not resolve placeholder 'KEY' in string value "${
错误。
Don't use multiple |
错误原因: 我在applicationContext-dao.xml和 redis-config.xml分别用了扫描器
<context:property-placeholder location="classpath:config/redis.properties"/>
如果再在这两个文件里扔一个扫描constant目录下属性文件的扫描器 那么同时存在两个:
此时要加上
ignore-unresolvable="true"
<context:property-placeholder location="classpath:config/redis.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:constant/constant.properties" ignore-unresolvable="true"/>
我又在applicationContext-service.xml 中又加载了一个属性文件
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:constant/constant.properties</value> </list> </property> </bean> 发现这个文件在我使用的时候@Value("${KEY}") 所注入的值直接是引号里面的 “${KEY}”,所以没有任何效果。
如果上面代码配置在springmvc文件中有效,且如果换成下面配置也有效,
<context:property-placeholder location="classpath:constant/constant.properties" ignore-unresolvable="true"/>
也就是说我不在同一个文件中使用<context:property-placeholder就不需要添加
ignore-unresolvable="true"