Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving system environment variable using Spring Expression Language

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?

like image 348
mkorszun Avatar asked May 27 '14 22:05

mkorszun


1 Answers

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" />
like image 178
Luiggi Mendoza Avatar answered Sep 18 '22 09:09

Luiggi Mendoza