$Value{}ntException: Could not resolve placeholder 'KEY' in string value "${

springmvc 中@Value("${KEY}")  出现 $Value{}ntException: Could not resolve placeholder 'KEY' in string value "${

 错误。

 


down vote

Don't use multiple <context:property-placeholder/> tags, refer below code for the same and also make sure you have a key "com.ibm.CORBA.securityServerHost" with a value in either of your property file.


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
      <list>
         <value>classpath:em-management.properties</value>
         <value>file:///opt/ass/swp/conf/platform.properties</value>
      </list>
   </property>
   <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

 

错误原因: 我在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"

 

 

 

 

posted @ 2016-11-26 13:02  cbam  阅读(146)  评论(0)    收藏  举报