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

    Comment


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

      Comment


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

        Comment


        • #5
          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, 05:52 PM.

          Comment

          Working...
          X