1

I want to use my system environment variables (i.e use variable FOO that I have defined in my .bash_profile export FOO=BAR) for values in Spring's applicationContext.xml;

For example:

<bean id="dataSource" ...>
   <property name="jdbcUrl" value="${FOO}" />
...
</bean>

I use both the Maven-tomcat-plugin and Tomcat inside IntelliJ to run my server.

Right now when my webapp starts up, it can't find the system variables.

What do I have to do to get Spring to read from my system environment variables?

P.S. I have already checked out how to read System environment variable in Spring applicationContext, but doesn't seem to work for me

Community
  • 1
  • 1
Glide
  • 20,235
  • 26
  • 86
  • 135

2 Answers2

2

System environment variables are different to the JVM system properties - which the spring PropertyPlaceholderConfigurer gives you access to. So you'd need to add to the JVM command:

java ... -DFOO=${FOO}

(where -DFOO is defining "FOO" as a JVM system property and ${FOO} is inserting the "BAR" value from your "FOO" environment variable)

See also the ServletContextPropertyPlaceholderConfigurer - with the SYSTEM_PROPERTIES_MODE_OVERRIDE property and a few other options.

kevinjansz
  • 638
  • 6
  • 20
1

try this (Spring Expression Language)

<bean id="dataSource" ...>
   <property name="jdbcUrl" value="#{systemEnvironment.FOO}" />
...
</bean>

or add this line

<context:property-placeholder/>

to application context

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275