Results 1 to 5 of 5

Thread: Environment Variables in Spring?

  1. #1
    Join Date
    Mar 2005
    Location
    Madrid, Spain
    Posts
    71

    Default 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. #2
    Join Date
    May 2005
    Location
    China
    Posts
    8

    Default

    Code:
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>/WEB-INF/jdbc.properties</value>
    			</list>
    		</property>
    	</bean>
    
    	<bean id="localDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="$&#123;jdbc.driverClassName&#125;"/>
    		<property name="url" value="$&#123;jdbc.url&#125;"/>
    		<property name="username" value="$&#123;jdbc.username&#125;"/>
    		<property name="password" value="$&#123;jdbc.password&#125;"/>
    	</bean>

  3. #3
    Join Date
    Mar 2005
    Location
    Madrid, Spain
    Posts
    71

    Default

    Thanks Nicholas for your replay, but I am asking for something diferent.

    Let say you want to refer some environment variable on the jdbc.properties file or directly on the file: myapplication-servlet.xml, for
    example, what about using: JAVA_HOME, environment variable, or any othe classical variable. Does spring load the environment variable information on the startup process? What is the syntax to use in order
    to refer them. In a similar way like Ant: ${java.home}, which is a system variable loaded on the ant startup process.

    I hope, now I have explained better,

    Thanks,

    David

  4. #4
    Join Date
    Apr 2005
    Location
    Portland, OR
    Posts
    8

    Default

    See the PropertyPlaceholderConfigurer Javadocs:

    "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"."

  5. #5
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    See also your other topic:

    http://forum.springframework.org/showthread.php?t=17174

    I think the conclusion is:

    -it is not a problem to use variables in Spring
    -it is not a problem to use system properties in Spring
    -but it is a problem to use environmental variables, because it is complicated to retrieve them.
    Last edited by robyn; May 15th, 2006 at 05:52 PM.

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Environment Variables in Spring?
    By dleal in forum Container
    Replies: 14
    Last Post: Apr 12th, 2007, 06:33 AM
  3. Replies: 0
    Last Post: Mar 8th, 2005, 10:00 PM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. spring in a clustered environment
    By kshacks in forum Container
    Replies: 1
    Last Post: Sep 29th, 2004, 07:50 PM

Posting Permissions

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