Announcement Announcement Module
Collapse
No announcement yet.
Can i use environment variable in applicationContext.xml? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can i use environment variable in applicationContext.xml?

    in my applicationContext.xml ,i use those to refer to jdbc connection


    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>jdbc.properties</value>
    			</list>
    		</property>
    	</bean>
    
    
        <!-- datasource   -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>$&#123;driverClassName&#125;</value></property>
    		<property name="url"><value>$&#123;driverClassName&#125;</value></property>
    		<property name="username"><value>$&#123;driverClassName&#125;</value></property>
    		<property name="password"><value>$&#123;driverClassName&#125;</value></property>		
        </bean>
    but now , i want to add environment variable in this file , that is to say, i want to use

    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>$&#123;env.XX_XX&#125;/jdbc.properties</value>
    			</list>
    		</property>
    	</bean>
    but it seems no effect , can i use env in appliactionContext.xml?

  • #2
    This is probably the only place where you cannot use this environment variable, since this is the place that variables get read.

    Like biting your own tail 8) .

    Comment


    • #3
      Hmm... did you specifically mean using a replacement property in your property configurer, or did you mean using an environment variable as a replacement property somewhere else in applicationcontext.xml?

      As the previous poster commented, it would seem unlikely to be able to use properties during the definition of the PropertyPlaceHolderConfigurer bean. (Although I haven't tried defining multiple PropertyPlaceHolderConfigurers...)

      However, as for the rest of applicationContext.xml, you still cannot use environment variables directly. The properties must be defined in the properties file specified in the locations attribute of the PropertyPlaceHolderConfigurer.

      You could, however, pass Java system properties on the command line to your application. The PropertyPlaceHolderConfigurer will fall back to looking at System properties if it can't find a property definition in any of your specified locations.

      For example, you could start your app with these parameters:
      Code:
          -Denv.my_var=%my_var%     &#40;for example, on Windows&#41;
      where my_var is an environment variable, then use
      Code:
         $&#123;env.my_var&#125;
      as a property in your applicationcontext.xml.

      Tim

      Comment

      Working...
      X