Spring @Value无法解析字符串值中的占位符

时间:2015-06-03 09:45:21

标签: java spring spring-mvc properties

让上下文像这样,我在我的上下文中有2个属性占位符。

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <context:annotation-config/>
    <context:component-scan base-package="com.xxx.app.xxx"/>

    <context:property-placeholder location="classpath:em-management.properties"/>
    <context:property-placeholder location="file:///opt/ass/swp/conf/platform.properties"/>

</beans>

当我运行代码时,遇到了这个错误:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CMClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.xxxx.app.xxxx.xx.client.CMClient.washost; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.ibm.CORBA.securityServerHost' in string value "${com.ibm.CORBA.securityServerHost}"

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.xxxx.app.xxx.xxx.client.CMClient.washost; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.ibm.CORBA.securityServerHost' in string value "${com.ibm.CORBA.securityServerHost}"

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'com.ibm.CORBA.securityServerHost' in string value "${com.ibm.CORBA.securityServerHost}"

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

我找到了针对此特定问题的解决方案,只需将ignore-unresolvable="true"附加到每行

<context:property-placeholder location="classpath:em-management.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="file:///opt/ass/swp/conf/platform.properties" ignore-unresolvable="true"/>

答案 1 :(得分:1)

请勿使用多个<context:property-placeholder/>代码,请参阅以下代码,并确保您的属性文件中包含一个值为"com.ibm.CORBA.securityServerHost"的密钥。

<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>