简体   繁体   中英

Setting Global Environmental Variables in Linux (Red Hat) for Java Web Application in Tomcat

We have externalized all properties in our application and reading it by setting environmental variables, like this..

<bean id="propertyPlaceholderConfigurer" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
    <constructor-arg ref="configurationEncryptor" />
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchSystemEnvironment" value="true"/>
    <property name="locations">
        <list>
            <value>file:${ENV_PROPS}/config/*.properties</value>
        </list>
    </property>
</bean> 

This above code works in both Windows and Mac, ie the environmental variable ENV_PROPS is correctly resolved.

However it fails to resolve in Linux (Red Hat). I have tried setting the environmental variables like this..

# /opt/application/config/database.properties is a valid path     
export ENV_PROPS=/opt/application 

in the following locations in Linux.

  1. /etc/profile.d/env_vars.sh

  2. /etc/profile

  3. /etc/bashrc

  4. /tomcat/conf/setenv.sh (for this, java code System.getenv works but xml fails to resolve)

It turns out to be an issue with the order in which applicationContext.xml is loaded in linux/tomcat and NOT environmental variable issues.

We have multiple context files in our application, just applying the above bean in main applicationContext.xml (which is also defined in web.xml) did not help.

We had to import the above bean as a resource into multiple xml files explicitly like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM