1

I'm trying to use an environment variable inside a Spring property file but it doesn't seem to work

where the app is running

export TEST_VAR=hello

myapp.properties

test.variable=${TEST_VAR}
enter code here

But when I test, the test.variable doesn't translate to hello but stays ${TEST_VAR}.

How can I get Env variable from within that file?

I load the property file this way

<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 is a system variable I pass when tomcat starts up through -DmyApp.configurationFile=...

Paulus2
  • 437
  • 5
  • 15
  • In the way of doing, I don't see any noticeable issue. You should provide a MCVE : a Minimal, Complete, and Verifiable Example. See https://stackoverflow.com/help/mcve – davidxxx Oct 10 '17 at 20:36
  • This is literally the only thing I do minus the way I instruct spring how to,oad the property file. I have added it to the post. – Paulus2 Oct 10 '17 at 20:56

1 Answers1

0

Adding to the configure place holder fixed my issue

<property name="searchSystemEnvironment" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
Paulus2
  • 437
  • 5
  • 15