2017-10-10 325 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" />