1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
fresh_dev
  • 6,694
  • 23
  • 67
  • 95
  • See Bozho's answer here: http://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext – atrain Jan 18 '12 at 14:43
  • How do you know it is not set? Also you should pass file/file pattern like `file:${My_ENV_VAR}/*.properties` not a directory. – mrembisz Jan 18 '12 at 15:13
  • sorry i updated the post, i know it's not set because after build i see the applicationContext and i can see that the value `file:${My_ENV_VAR}` is not changed. – fresh_dev Jan 18 '12 at 15:20
  • It won't be substituted during build, spring will do it in runtime. If you want it to be substituted in build time, configure maven filtering or equivalent. – mrembisz Jan 18 '12 at 15:23
  • how to configure maven to do that please ? – fresh_dev Jan 18 '12 at 15:42
  • Here is link: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html – mrembisz Jan 18 '12 at 15:51

2 Answers2

8

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() -->
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • sorry i don't understand, i want the property placeholder to point to a property file it's value is read from an environment variable, such thing is not doable ? – fresh_dev Jan 18 '12 at 14:43
  • i added an update for the requirement if there's another solution for what i am trying to do. – fresh_dev Jan 18 '12 at 14:46
  • @fresh_dev the thing is: you are using the PropertyPlaceholderConfigurer syntax while you are creating it. – Sean Patrick Floyd Jan 18 '12 at 14:52
  • it doesn't work, what do i need to get spring expression language to work beside it's jar ? do i need to add anything in `beans xmlns` declaration? – fresh_dev Jan 18 '12 at 15:18
  • @fresh_dev uhm, what Spring version are you using? In 3.x it should work out of the box... – Sean Patrick Floyd Jan 18 '12 at 15:37
  • 1
    i am using version 3.0.5.RELEASE – fresh_dev Jan 18 '12 at 15:39
  • but BTW, will the value be replaced during runtime not build time ? and how to check that the value is replaced if it's replaced during runtime ? – fresh_dev Jan 18 '12 at 15:43
  • i am getting an exception now in runtime: `org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 20): Method call: Method getEnv(java.lang.String) cannot be found on java.lang.System type` – fresh_dev Jan 18 '12 at 15:49
  • what do you think about this exception ? – fresh_dev Jan 18 '12 at 15:59
  • @fresh_dev my bad, it's System.getenv() – Sean Patrick Floyd Jan 18 '12 at 16:17
  • well this exception is gone, but application cannot start yet, i am getting ClassNotFoundException for a class that's already in the target ! so how to make sure that this code works fine since it's getting applied during runtime then ? – fresh_dev Jan 19 '12 at 08:31
  • @fresh_dev post your stack trace in a new question – Sean Patrick Floyd Jan 19 '12 at 08:53
1

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

schnittstabil
  • 643
  • 7
  • 14
Rene Enriquez
  • 1,418
  • 1
  • 13
  • 33