views:

74

answers:

4

How to read the system environment variable in the application context?

I want something like :

<util:properties id="dbProperties"
        location="classpath:config_DEV/db.properties" />

or

<util:properties id="dbProperties"
        location="classpath:config_QA/db.properties" />

depending on the environement.

Can I have something like this in my application Context?

<util:properties id="dbProperties"
        location="classpath:config_${systemProperties.env}/db.properties" />

where the actual val is set based on the SYSTEM ENVIRONMENT VARIABLE

I'm using Spring 3.0

A: 

One tweak I can see is,

Pass parameter to jvn using
for example

-Denv.ant_home_var=%ANT_HOME%   

Then you can configure PropertyPlaceHolder to achieve your goal.

Or you can directly use

${env.ant_home_var} in your xml file

org.life.java
A: 

Yes, you can do <property name="defaultLocale" value="#{ systemProperties['user.region']}"/> for instance.

The variable systemProperties is predefined, see 6.4.1 XML based configuration.

Istao
+2  A: 

You are close :o) Spring 3.0 adds Spring Expression Language. You can use

<util:properties id="dbProperties" 
    location="classpath:config_#{systemProperties['env']}/db.properties" />

Combined with java ... -Denv=QA should solve your problem.

amra
+2  A: 

Check this article. It gives you several ways to do this, via the PropertyPlaceholderConfigurer which supports external properties (via the systemPropertiesMode property)

Bozho