0

Config in applicationContext.xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/jdbc-${WEB_ENVIRONMENT}.properties</value>
                <value>classpath:/settings-${WEB_ENVIRONMENT}.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"></property>
        <property name="searchSystemEnvironment" value="true"></property>
</bean>

I have set the environment variable in both my user's & root's .bashrc file like so

export WEB_ENVIRONMENT=prod

On Starting tomcat I'm getting the error

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [jdbc-${WEB_ENVIRONMENT}.properties] cannot be opened because it does not exist

I also tried it this way:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/jdbc-#{T(java.lang.System).getenv('WEB_ENVIRONMENT')}.properties</value>
                <value>classpath:/settings-#{T(java.lang.System).getenv('WEB_ENVIRONMENT')}.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"></property>
        <property name="searchSystemEnvironment" value="true"></property>
</bean>

This time I get the error

Caused by: java.io.FileNotFoundException: class path resource [jdbc-.properties] cannot be opened because it does not exist

Which means that SpEL is functional inside the applicationContext.xml but not able to fetch variables from the System environment.

To check if value is correctly set I ran echo ${WEB_ENVIRONMENT} and it correctly returns the value. So, to check if java is able to fetch the value from the environment I ran

public class Test {

    public static void main(String[] args) {
        System.out.println(System.getenv("WEB_ENVIRONMENT"));
    }

}

This also correctly returned "prod"

Both version run in Windows, but do not in Amazon EC2's Linux AMI.

What should I try next to get it to work?

Varun Achar
  • 13,274
  • 7
  • 53
  • 71
  • Also note, `#{systemProperties['WEB_ENVIRONMENT']}` doesn't work either – Varun Achar Mar 06 '13 at 19:10
  • How are you starting tomcat in EC2? – beny23 Mar 06 '13 at 19:47
  • sudo service tomcat7 start. Though would prefer to have it set in the environment rather than passing -D arguments – Varun Achar Mar 06 '13 at 19:49
  • 1
    And which user is running tomcat (look for the java processing in `ps auxwww`)? I think that the env var isn't picked up, e.g. if bash isn't used to start tomcat or a different user is running tomcat. – beny23 Mar 06 '13 at 19:53
  • tomcat is also installed as a service with runs on start up. Somehow `ps auxwww` shows tomcat as the user running tomcat! I'm a noob at linux so dunno what that means.. – Varun Achar Mar 06 '13 at 19:58
  • Have a look at [this question](http://stackoverflow.com/questions/5115339/tomcat-opts-environment-variable-and-system-getenv) – Arne Burmeister Mar 06 '13 at 20:06
  • when the tomcat is running as user tomcat, it doesn't read your .bashrc, have a look at the tomcat config (catalina.sh, ...) and add your env there – Arne Burmeister Mar 06 '13 at 20:13
  • @VarunAchar: or add it to /etc/profile which adds the env variable globally, though personally I would go with Arne's suggestion. – beny23 Mar 06 '13 at 20:15
  • Thanks guys! That worked.. Phew.. – Varun Achar Mar 06 '13 at 20:21

2 Answers2

1

Few options that I know,

  1. Try to set it as VM Arguments when you start the application. i.e. -DWEB_ENVIRONMENT=prod

  2. In case of Tomcat you can set it in JAVA_OPTS see this answer to find out how to achieve that.

  3. In my own project I set my environment variable value in catalina.properties at very end by putting -Druntime_env=dev.

Community
  • 1
  • 1
Anuj Patel
  • 15,269
  • 2
  • 26
  • 55
1

For the first attempt you have a hen and egg problem, I think. You want to configure the PropertyPlaceholderConfigurer using values that need to be replaced with properties. This wont work!

The SpEL approach should work, I just tried a similar config on my mac (UNIX like system) with a value listed by printenv on the shell (USER). The result is as expected:

java.io.FileNotFoundException: class path resource [settings-arne.properties] cannot be opened because it does not exist

You can set the environment variable as suggested here. Another way is to add the variable to add it to /etc/profile which adds the env variable globally. The former is preferred.

Community
  • 1
  • 1
Arne Burmeister
  • 18,467
  • 8
  • 50
  • 89