2017-10-10 327 views
0

我试图使用弹簧属性文件中的环境变量,但它似乎没有工作环境变量

,其中应用程序正在运行

export TEST_VAR=hello 

myapp.properties

test.variable=${TEST_VAR} 
enter code here 

但是当我测试时,test.variable不会转化为hello,而是保持$ {TEST_VAR}。

如何从该文件中获取Env变量?

我加载属性文件,这样

<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="singleton" value="true" /> 
    <property name="ignoreResourceNotFound" value="false" /> 
    <property name="locations"> 
     <list> 
     <value>classpath:myapp.properties</value> 
     <value>file://#{systemProperties['myApp.configurationFile']}</value> 
     </list> 
    </property> 
    </bean> 

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> 
    <property name="properties" ref="props" /> 
    </bean> 

    <bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="properties" ref="props" /> 
    </bean> 

myApp.configurationFile是一个系统变量我通过当Tomcat通过-DmyApp.configurationFile = ...

+0

在做的方式,我没有看到任何明显的问题。您应该提供一个MCVE:一个最小,完整和可验证的示例。请参阅https://stackoverflow.com/help/mcve – davidxxx

+0

这实际上是我唯一做的事,就是减去我指示spring如何填写属性文件的方式。我已将它添加到帖子中。 – Paulus2

回答

0

启动添加到配置占位符固定我的问题

<property name="searchSystemEnvironment" value="true" /> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />