Announcement Announcement Module
Collapse
No announcement yet.
OS Environment variables for Property Placeholders Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OS Environment variables for Property Placeholders

    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

  • #2
    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,

    Comment


    • #3
      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.

      Thanks for the reply...

      --pete

      Comment


      • #4
        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.

        Comment


        • #5
          Re: OS Environment variables for Property Placeholders

          Originally posted by pmorelli
          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:
          Code:
            <bean id="sysInitParameters"
                  class="net.sourceforge.jwebutil.util.initparameters.InitParametersFactory"
                  factory-method="createSystemPropertiesInstance">
              <description>System Properties Init Parameters</description>
            </bean>
          2. Create a bean, initializing it with the appropriate System Properties:

          Code:
          <bean id="myBean" class="net.sourceforge.jwebutil.util.initparameters.PropertyMapper">
              <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:

          Code:
            PropertyMapManager.DEFAULT.refresh(myBean, true);
          Last edited by Colin Sampaleanu; Nov 21st, 2005, 09:48 PM.

          Comment

          Working...
          X