Announcement Announcement Module
Collapse
No announcement yet.
Environment Variables in Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Environment Variables in Spring?

    I would like to know how to access to an environment variable in Spring. For example I would like to use some environment variable in order to set the url property:

    Code:
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
          <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property> 
          <property name="url"> 
            <value>jdbc&#58;hsqldb&#58;P&#58;/springapp/v6/springapp/db/test</value> 
          </property> 
        </bean>
    but I don't know which environment variables are accepted by Spring configuration files and how to use them (syntax), for example: ${user.home}, $HOME, etc. Any way are there any Spring Environment variables, could be used on the Spring configuration files.

    Thanks in advance,

    David Leal

  • #2
    Take a look at the:
    http://www.springframework.org/docs/...onfigurer.html

    and
    http://www.springframework.org/docs/...postprocessors


    Btw:
    it would be nice if I could evaluate some expressions in Spring (maybe by using something like ognl), so I can say:

    <property name="timeout" value="ognl:60*60*24"/>

    and even something like:

    <property name="timeout" value="ognl:60*60*${hours}*fooBean.days"/>

    Comment


    • #3
      Thanks Alarmnummer,

      I am going to take a detailed look to this information, but I am afraid that
      I am asking about somethink different.

      What I like is to have access to some System Environment Variables. For example to refer one of the following system variables that shows the set dos command:

      Code:
      c&#58;\> set
      
      ALLUSERSPROFILE=C&#58;\Documents and Settings\All Users
      ANT_HOME=c&#58;\archiv~1\Java\Apache\Ant\ant
      APPDATA=C&#58;\Documents and Settings\david\Datos de programa
      APR_ICONV_PATH=C&#58;\Archivos de programa\Subversion\iconv
      CLASSPATH=.;S&#58;\java\class;S&#58;\java\lib;S&#58;\java\resources
      CommonProgramFiles=C&#58;\Archivos de programa\Archivos comunes
      COMPUTERNAME=BOSTON4BDRCHA
      ComSpec=C&#58;\WINDOWS\system32\cmd.exe
      FP_NO_HOST_CHECK=NO
      HOMEDRIVE=C&#58;
      HOMEPATH=\Documents and Settings\david
      JAVA_HOME=C&#58;\Java\j2sdk\j2sdk1.4.2_08
      LOGONSERVER=\\BOSTON4BDRCHA
      MuPAD_SWP_200=C&#58;\Archivos de programa\swp40\MuPAD
      MY_CLASSPATH=S&#58;\java\class;S&#58;\java\lib;S&#58;\java\resources
      MY_PATH=c&#58;\archiv~1\Java\Apache\Ant\ant\bin;c&#58;\archiv~1\cvs;C&#58;\Java\j2sdk\bin
      NUMBER_OF_PROCESSORS=1
      OS=Windows_NT
      Path=c&#58;\archiv~1\MiKTeX\Main\miktex\bin;c&#58;\archiv~1\Java\Apache\Ant\ant\bin;c&#58;\archiv~1\cvs;C&#58;\Java\j2sdk\bin;C&#58;\WINDOWS\system32;C&#58;\WINDOWS;C&#58;\WINDOWS\System32\Wbem;C&#58;\ARCHIV~1\ULTRAE~1;c&#58;\archiv~1\matlab6p5\bin\win32;c&#58;\archiv~1\Subversion\bin
      PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
      PROCESSOR_ARCHITECTURE=x86
      PROCESSOR_IDENTIFIER=x86 Family 6 Model 9 Stepping 5, GenuineIntel
      PROCESSOR_LEVEL=6
      PROCESSOR_REVISION=0905
      ProgramFiles=C&#58;\Archivos de programa
      PROMPT=$P$G
      SESSIONNAME=Console
      SystemDrive=C&#58;
      SystemRoot=C&#58;\WINDOWS
      TEMP=C&#58;\DOCUME~1\david\CONFIG~1\Temp
      TMP=C&#58;\DOCUME~1\david\CONFIG~1\Temp
      USERDOMAIN=BOSTON4BDRCHA
      USERNAME=david
      USERPROFILE=C&#58;\Documents and Settings\david
      windir=C&#58;\WINDOWS
      What about if I want to use the system environment variable HOMEPATH on the myapp-servlet.xml. Is there any way to get a reference to this variable?

      Thanks in adavance,

      David

      Comment


      • #4
        Originally posted by dleal
        Thanks Alarmnummer,

        I am going to take a detailed look to this information, but I am afraid that
        I am asking about somethink different.

        What I like is to have access to some System Environment Variables.
        I think that is going to be complicated, see topics at:
        http://www.google.nl/search?hl=nl&q=...e+zoeken&meta=

        It seems there is no good way in Java to access them.

        Btw:
        you can access the system.properties with the links I have given you.

        Comment


        • #5
          I believe PropertyPlaceHo.... accesses the environment system properties too.

          "....A configurer will also check against system properties (e.g. "user.dir") if it cannot resolve a placeholder with any of the specified properties. This can be customized via "systemPropertiesMode". "

          Comment


          • #6
            Originally posted by jbetancourt
            I believe PropertyPlaceHo.... accesses the environment system properties too.

            "....A configurer will also check against system properties (e.g. "user.dir") if it cannot resolve a placeholder with any of the specified properties. This can be customized via "systemPropertiesMode". "
            Yes.. but the question is if you can access the environmental variables. And I don`t think this is going to be easy: check the google link.

            Comment


            • #7
              The google links are addressing getEnv(), which is deprecated. However, the javadoc for System.getProperty(String key), seems to still be recommended.

              And, Spring uses that: propVal = System.getProperty(placeholder);

              Of course, there are many issues with Java accessing system level stuff, but that is is where other languages really shine like Perl, Python, etc.

              Comment


              • #8
                Originally posted by jbetancourt
                The google links are addressing getEnv(), which is deprecated. However, the javadoc for System.getProperty(String key), seems to still be recommended.
                Yes.. but the question is: can you find the environmental variables in the System.properties.

                Comment


                • #9
                  No, System.getProperty will not return environment variables. System.getEnv will (which isn't deprecated in 1.5).

                  Comment


                  • #10
                    I just looked at both 1.4 and 1.5 api docs. System does have many differences, one of these is that getenv is not deprecated in 1.5. Fun.

                    Comment


                    • #11
                      Dear Members,

                      I have read all the replies and I want to reproduce what you say, but I have stoped just at the begining. The source code example:

                      Code:
                      XmlBeanFactory factory = new XmlBeanFactory&#40;new FileSystemResource&#40;"beans.xml"&#41;&#41;;
                      // create placeholderconfigurer to bring in some property
                      // values from a Properties file
                      PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer&#40;&#41;;
                      cfg.setLocation&#40;new FileSystemResource&#40;"jdbc.properties"&#41;&#41;;
                      // now actually do the replacement
                      cfg.postProcessBeanFactory&#40;factory&#41;;
                      Assumes the user is getting the XmlBeanFactory, on my test class I am using Application context:
                      Code:
                      ApplicationContext ac = new FileSystemXmlApplicationContext&#40;"springapp-servlet.xml"&#41;
                      and I don't see the way to modify the properties loaded on the application context, because the postProcessBeanFactory method doesn't accept an instance of the ApplicationContext. It expect an instance of ConfigurableListableBeanFactory and I don't see the way to get the corresponding XmlBeanFactory from the ApplicationContext.

                      I have another question, as far as I understand you have to intercept or modiefy the loaded properties using hard code (via java source code), but how to change such properties when the application starts, because we can have more than one servlet and I don't think it is a way to load some properties using some XML directive on the springapp-servlet.xml file. It could be a good suggestion for next versions

                      Thanks in advance,

                      David

                      Comment


                      • #12
                        Originally posted by dleal
                        and I don't see the way to modify the properties loaded on the application context, because the postProcessBeanFactory method doesn't accept an instance of the ApplicationContext. It expect an instance of ConfigurableListableBeanFactory and I don't see the way to get the corresponding XmlBeanFactory from the ApplicationContext.
                        The ConfigurableApplicationContext interface has a "getBeanFactory()" method that returns the underlying ConfigurableListableBeanFactory.

                        However, note that BeanFactoryPostProcessors are usually defined as beans loaded by the ApplicationContext, getting autodetected and applied on startup. This is one of the value-adds that an ApplicationContext has over a plain BeanFactory.

                        With a plain BeanFactory, you need to apply a BeanFactoryPostProcessor programmatically (same applies for a BeanPostProcessor, which needs to be registered programmatically). With an ApplicationContext, this is usually not necessary because of the autodetection mechanism.

                        Juergen

                        Comment


                        • #13
                          FYI, I've just added a "searchSystemEnvironment" bean property to PropertyPlaceholderConfigurer, activating an additional check against system environment variables after checking JVM system properties.

                          Environment variables will be checked at the configured time of the system properties mode, i.e. either overriding local properties or as fallback after checking local properties.

                          As a consequence, placeholders like ${JAVA_HOME} will work now, provided that PropertyPlaceholderConfigurer is configured with "searchSystemEnvironment=true".

                          This will be included in the upcoming Spring 1.2.4.

                          Juergen

                          Comment


                          • #14
                            Is there any work-around to use this with jdk 1.4, since System.getEnv() throws an Error?

                            Besides, is it possible to pass the path to the .properties file using one of those environment properties? I mean:

                            Code:
                            	<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                            		<property name="location" value="file:${MYENVPROPERTY}/conf/myconfig.properties" />
                            		<property name="searchSystemEnvironment" value="true"/>
                            	</bean>

                            Comment


                            • #15
                              I would have thought the only work around is to define the property in a properties file, pass it in as a system property or use Java 1.5+.

                              Comment

                              Working...
                              X