9

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?

1 Answer 1

7

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" /> 
Sign up to request clarification or add additional context in comments.

6 Comments

would this work with OS system variables? I expect it to work only with jvm properties - anyway I have tried it and my variable is resolved again to empty string.
Spring provides both systemProperties and systemEnvironment.
@SotiriosDelimanolis where did you find that? The only reference I can find is an oblique reference at Spring API which I assume (but it's not explicit) relates to SpEL and says 'systemEnvironment' is from the OS and 'systemProperties' is from the JVM. My brief test confirms the latter, but I can't get the Windows envvars with systemEnvironment.
@SotiriosDelimanolis found it - it's in Spring API entry for PropertySourcesPlaceHolderConfigurer and I was unwittingly using the out-of-date plain ol' PropertyPlaceholderConfigurer. Had to upgrade the schema ref to spring-context-3.1.xsd and the propertyConfigurer class to org.springframework.context.support.PropertySourcesPlaceholderConfigurer and suddenly it worked. Spent way too long googling on "SpEL"
@Adam Take a look at StandardEnvironment as well. It is the base class of most Environment types.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.