Announcement Announcement Module
No announcement yet.
Environment Variables in Spring? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
          <property name="url">
    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
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    	<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;"/>


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

      Let say you want to refer some environment variable on the 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,




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


        • #5
          See also your other topic:

          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.