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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.