Resolving system environment variable using Spring Expression Language

environment-variables, spring, spring-el

Solution

The SpEL variable is `systemProperties`, not `systemEnvironment`.

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.#{systemProperties['THREAD_ENV']}.properties" />

Problem

I want to resolve system environment variable using `Spring Expression Language' in spring servlet configuration file. My first approach was: ``` <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.#{systemEnvironment.THREAD_ENV}.properties" /> ``` This is throwing below exception: ``` Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 18): Field or property 'THREAD_ENV' cannot be found on object of type 'java.util.Collections$UnmodifiableMap' ``` Then I have tried: ``` <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.#{systemEnvironment['THREAD_ENV']}.properties" /> ``` and ``` <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.#{systemEnvironment[THREAD_ENV]}.properties" /> ``` which both fail and resolve to empty string. I am using Tomcat6 and I export this variable just before restarting Tomcat server: ``` export THREAD_ENV=live; /etc/init.d/tomcat6 restart; ``` Would like to mention that all three methods work on some of my Tomcat6 instances but not on all - what could be the reason of such a strange behaviour? Any idea what I am doing wrong?

Original source