fresh_dev
fresh_dev

Reputation: 6774

propertyPlaceHolderConfigurer and environment variable

I am trying to load a property file from an environment variable, so here's what I tried:

<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>

                <value>classpath:messages/application.properties</value>
                <value>file:${My_ENV_VAR}/*.properties</value>

            </list>
        </property>

        <property name="ignoreResourceNotFound" value="true" />
        <property name="searchSystemEnvironment" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />

    </bean>

I have a new environment variable named My_ENV_VAR=C:\Program Files\My Folder\props.properties but when stopping and starting the application the value of the variable is not set, any ideas why?

UPDATE: Requirement

I want to read the hibernate properties (url,username,password) in the applicationContext.xml from an external property file on file system, which its path is stored in an environment variable.

Upvotes: 1

Views: 6946

Answers (2)

Rene Enriquez
Rene Enriquez

Reputation: 1599

Yo should be use of this manner:

First declare the spring bean

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>             
            <value>classpath:config.properties</value>
        </list>
    </property>
</bean>

Now in the WEB-INF/classes directory create the file config.properties and put this:

jboss.variable=${jboss.modules.dir}

Note: When I deploy JBoss 6 EAP the log shows me:

jboss.modules.dir = C:\Java\jee-container\jboss-eap-6.1\modules

and use the variable in application context file:

<bean id="nameOfBean"
    class="com.moeandjava.pusku.MySpringBean">
    <property name="path" value="${jboss.variable}" />
</bean>

Sorry for my bad english

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

You are trying to use the PropertyPlaceholderConfigurer to create the PropertyPlaceholderConfigurer. That's a chicken / egg problem, it can't work!

Try expression language instead (see this section for reference), but in your case it's tricky because you want to mix static and dynamic content. Probably something like this will work:

<property name="locations"
  value="classpath:messages/application.properties,
  #{ T(java.lang.System).getenv('MY_ENV_VAR')}" />
  <!-- changed method name, it's getenv(), not getEnv() -->

Upvotes: 8

Related Questions