Glide
Glide

Reputation: 21245

Spring to read from my system environment variables

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

Upvotes: 1

Views: 4823

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

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

Upvotes: 1

kevinjansz
kevinjansz

Reputation: 648

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.

Upvotes: 2

Related Questions