propertyPlaceHolderConfigurer and environment variable

末鹿安然 提交于 2019-12-11 05:25:35

问题


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.


回答1:


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() -->



回答2:


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



来源:https://stackoverflow.com/questions/8911602/propertyplaceholderconfigurer-and-environment-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!