Results 1 to 5 of 5

Thread: OS Environment variables for Property Placeholders

  1. #1
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    18

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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,

  3. #3
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    18

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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.

  5. #5
    Join Date
    Aug 2004
    Location
    California
    Posts
    17

    Default Re: OS Environment variables for Property Placeholders

    Quote 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 at 08:48 PM.

Similar Threads

  1. System configuration per environment
    By kbaum in forum Architecture
    Replies: 20
    Last Post: Sep 29th, 2009, 04:06 PM
  2. Environment Variables in Spring?
    By dleal in forum Container
    Replies: 14
    Last Post: Apr 12th, 2007, 06:33 AM
  3. Environment Variables in Spring?
    By dleal in forum Data
    Replies: 4
    Last Post: Aug 9th, 2005, 04:58 PM
  4. spring in a clustered environment
    By kshacks in forum Container
    Replies: 1
    Last Post: Sep 29th, 2004, 07:50 PM
  5. Named Bind Variables
    By james.estes in forum Data
    Replies: 4
    Last Post: Sep 6th, 2004, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •