PDA

View Full Version : OS Environment variables for Property Placeholders


pmorelli
Aug 12th, 2004, 05:58 PM
Is there any way in spring to replace property placeholders in bean definition files with values from environment variables, similar to the ant ${osenv.*} tags?

I know PropertyPlaceholderConfigurer will start with property files and then go to System Properties, but I'd rather not have to pass in all my env vars in as system properties with a -D on the command line.

Thanks...

--pete

Colin Sampaleanu
Aug 12th, 2004, 06:58 PM
Pete,

There's no standard way in Java to access environment variables. Ant for example, actually gets the shell to spit out its vars, and parses them (taking account differences between windows, unix, and cygwin), in order to access env variables.

This is not something I think we want to get into, in Spring.

Regards,

pmorelli
Aug 12th, 2004, 07:07 PM
Yes, I know. I was poking around the ant source code, and they have callouts for several oses, even VMS! But you're right, it's a little messy.

If I can't work around my env requirements some other way, I was going to try to extend PropertyPlaceholderConfigurer with the ant osenv code. I'll post it if I do go that route, in case anyone else needs it...

Also, fyi, JDK 1.5 is bringing back System.getEnv(). Maybe Spring 3.0 can use it. :D

Thanks for the reply...

--pete

jbetancourt
Sep 11th, 2004, 01:38 AM
A quick hack is to just redirect the environment settings into a prop file as part of a start up script. Then using that prop file in the usual manner.

In windows:

set > systemEnv.properties

A grep or sed expression can also be used in a pipe filter to manipulate them of course, like adding prefixes.

Of course, not dynamic, unless a further hack is done to call an exec from the application to do another grab.

MHarhen
Sep 11th, 2004, 07:30 PM
Is there any way in spring to replace property placeholders in bean definition files with values from environment variables, similar to the ant ${osenv.*} tags?
The code used at http://forum.springframework.org/showthread.php?t=9640 can be used to initialize a bean using System Properties.

1. Create an InitParameters object:
<bean id="sysInitParameters"
class="net.sourceforge.jwebutil.util.initparameters.InitP arametersFactory"
factory-method="createSystemPropertiesInstance">
<description>System Properties Init Parameters</description>
</bean>
2. Create a bean, initializing it with the appropriate System Properties:

<bean id="myBean" class="net.sourceforge.jwebutil.util.initparameters.Prope rtyMapper">
<property name="targetClass"><value>...MyBean</value></property>
<property name="sysInitParameters"><ref bean="properties" /></property>
<property name="properties">
<props>
<prop key="javaVersion">java.version</prop>
<prop key="runtimeName">java.runtime.name</prop>
</props>
</property>
</bean>
3. You can refresh the bean dynamically at any time using the current version of the System Properties:

PropertyMapManager.DEFAULT.refresh(myBean, true);