tags:

views:

286

answers:

3

I have a requirement to have all our properties files be stored in a directory. The location of this directory should be stored in a system environment variable. In my application context I will need to access this environment variable to create the FileSystemResource bean. Here is an example of what I would normally have:

<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg>
                <value>myprops.properties</value>
            </constructor-arg>
        </bean>
    </property>
</bean>

Instead I will need to have it be something like

<value>${prop_file_location}/myprops.properties</value>

Where prop file location is an environment variable. Does anyone know an easy way of doing this?

I am using spring 2.5.6 and java 1.6

+1  A: 

You could always extend the FileSystemResource (i.e. PropertiesFileResource) that would initialize itself by taking prepending the property file location system property to the file path.

Sanjay Ginde
I thought about doing this, but I had three separate projects that I needed to do the same thing with. Figured a snippet of xml was better than implementing the same class 3 times.
predhme
+1  A: 

I ended up going with a non programmatic approach. I used a MethodInvoker to retrieve the environment value. I was able to then pass that into the FileSystemResource.

<bean id="configPath" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
    <property name="targetClass" value="java.lang.String" />
    <property name="staticMethod" value="java.lang.System.getenv" />
    <property name="arguments">
        <list>
            <value>NAME_OF_VARIABLE</value>
        </list>
    </property>
</bean>
predhme
+1  A: 

In Spring.Net we have got the IVariableSource interface and PropertyPlaceholderConfigurer which are able to retrieve values from environment variables. Maybe there is something similar in the Spring Framework for Java?

Edit: I think I found the corresponding java bean which is named PropertyPlaceholderConfigurer as well in the java docs.

tobsen