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?