Results 1 to 5 of 5

Thread: Using Environment Variables to filter PropertyPlaceholderConfigurer locations

  1. #1

    Default Using Environment Variables to filter PropertyPlaceholderConfigurer locations

    I have a remote service which manages all the properties files for all my Java applications, now this service has 3 instances, one for each of our environments, Development, Beta and Live.

    I want to be able to provide the base URL for the remote locations using Windows/Linux environment variables but I'm running in to some problems.

    Code:
      	<bean name="databaseProperties" class="com.idna.config.springsupport.BasicHttpAuthenticatedUrlResource">
    		<constructor-arg value="http://${CONFIG_SERVICE}/properties/databases.xml" />
    		<property name="credentials" ref="autowiredCredentials" />
    	</bean>
    	
            <bean name="loggingProperties" class="com.idna.config.springsupport.BasicHttpAuthenticatedUrlResource">
    		<constructor-arg value="http://${CONFIG_SERVICE}/properties/logging.xml" />
    		<property name="credentials" ref="autowiredCredentials" />
    	</bean>
    	
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<ref bean="databaseProperties" />
    				<ref bean="loggingProperties" />
    			</list>
    		</property>
    		<property name="ignoreResourceNotFound" value="true" />
    		<property name="searchSystemEnvironment" value="true" />
    		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    	</bean>
    
    	<bean class="com.idna.linkeddata.domain.PropertyTest" >
        	        <property name="configUrl" value="${CONFIG_SERVICE}" />
             	<property name="test" value="${corporate.database.username}" />
            </bean>
    The problem I have is that I can load the environment variable CONFIG_SERVICE (a simple url) and inject it to my test bean (PropertyTest) when I hard code the URL's to databaseProperties and loggingProperties beans. But not if they are filtered as shown above because to load CONFIG_SERVICE I need to instantiates the PropertyPlaceholder but obviously I can't do this without filtering the locations first. It's a catch 22.

    I have tried adding another property placeholder but this will overwrite the one I want and not provide any of the remote locations.

    Any ideas ?

    Thanks

  2. #2
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Lightbulb Use Spel for this

    instead of using the expression ${CONFIG_SERVICE} to access an environment variable CONFIG_SERVICE, use the Spel expression #{systemProperties['CONFIG_SERVICE']}. Also remember to read the online documentation here

  3. #3

    Default

    This doesn't work as the SPEL doesn't seem to get a handle on the windows environment variables such as CONFIG_SERVICE , JAVA_HOME or Path. I've tried it programmatically and using annotations. It resolves the expression and returns an empty string. I am using spring 3.1 so SPEL works, it is just not retrieving windows environment variables.

    Just to test I have 1 bean created and autowire it in a test class.

    Code:
    	
    
        <bean class="com.idna.linkeddata.domain.PropertyTest" >
        	<property name="configUrl" value="#{ systemProperties['CONFIG_SERVICE'] }" />
        	<property name="random" value="#{ T(java.lang.Math).random() * 100.0 }" />
        </bean>
    configUrl comes out as null, random works as expected.

  4. #4
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    I ain't sure about windows as I don't have a Windows machine but it should work for sure. How are you defining environment variables in windows? one difference might be the use of
    <contextroperty-placeholder>
    which I assume that you're using. If instead using a
    <bean>
    approach, can you also try other approach and confirm?

  5. #5

    Default

    Okay so it was a quick fix.

    Instead of using systemProperty I changed it to systemEnvironment and everything is working as expected.

    Thanks for your help, much appreciated.

    Here is my complete config for anyone that was interested.

    Code:
    	<!-- Properties Store Configuration --> 
      	
      	<bean name="databaseProperties" class="com.idna.config.springsupport.BasicHttpAuthenticatedUrlResource">
    		<constructor-arg value="#{ systemEnvironment['CONFIG_SERVICE'] }properties/databases.xml" />
    		<property name="credentials" ref="autowiredCredentials" />
    	</bean>
    	
            <bean name="loggingProperties" class="com.idna.config.springsupport.BasicHttpAuthenticatedUrlResource">
    		<constructor-arg value="#{ systemEnvironment['CONFIG_SERVICE'] }properties/logging.xml" />
    		<property name="credentials" ref="autowiredCredentials" />
    	</bean>
    
    	<bean name="autowiredCredentials"
    		class="com.idna.config.springsupport.AuthenticationCredentials">
    		<property name="username" value="username" />
    		<property name="password" value="password" />
    	</bean>
    	
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
    		<property name="locations">
    			<list>
    				<ref bean="databaseProperties" />
    				<ref bean="loggingProperties" />
    			</list>
    		</property>
    		<property name="ignoreResourceNotFound" value="true" />
    		<property name="searchSystemEnvironment" value="true" />
    		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    	</bean>
    	 
    	<bean class="com.idna.linkeddata.domain.PropertyTest" >
        	    <property name="configUrl" value="#{ systemEnvironment['CONFIG_SERVICE'] }" />
        	    <property name="random" value="#{ T(java.lang.Math).random() * 100.0 }" />
        	    <property name="test" value="${corporate.database.username}" />
            </bean>

Posting Permissions

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