How to read system environment variable in Spring applicationContext

How to read a system environment variable in an application context?

I need something like:

<util:properties id="dbProperties" location="classpath:config_DEV/db.properties" /> 

or

 <util:properties id="dbProperties" location="classpath:config_QA/db.properties" /> 

depending on the environment.

Can I have something like this in my application Context?

 <util:properties id="dbProperties" location="classpath:config_${systemProperties.env}/db.properties" /> 

where the actual val is set based on the SYSTEM ENVIRONMENT VARIABLE parameter

I am using Spring 3.0

+95
java spring environment-variables
Oct 19 '10 at 4:57
source share
12 answers

Check out this article . This gives you several ways to do this through a PropertyPlaceholderConfigurer that supports external properties (through the systemPropertiesMode property).

+46
Oct 19 '10 at 6:07
source share

You are close: o) Spring 3.0 adds Spring Expression Language . you can use

 <util:properties id="dbProperties" location="classpath:config_#{systemProperties['env']}/db.properties" /> 

Combined with java ... -Denv=QA should solve your problem.

Also note the @yiling comment:

To access the system environment variable, these are OS level variables, as amoe commented, we can simply use "systemEnvironment" instead of "systemProperties" in this EL. like #{systemEnvironment['ENV_VARIABLE_NAME']}

+99
Oct 19 '10 at 6:02
source share

You can currently install

 @Autowired private Environment environment; 

in your @Component , @Bean , etc., and then access the properties through the Environment class:

 environment.getProperty("myProp"); 



For a single property in @Bean

 @Value("${my.another.property:123}") // value after ':' is the default Integer property; 



Another way is the convenient @ConfigurationProperties beans:

 @ConfigurationProperties(prefix="my.properties.prefix") public class MyProperties { // value from my.properties.prefix.myProperty will be bound to this variable String myProperty; // and this will even throw a startup exception if the property is not found @javax.validation.constraints.NotNull String myRequiredProperty; //getters } @Component public class MyOtherBean { @Autowired MyProperties myProperties; } 

Note. Remember to restart eclipse after setting a new environment variable

+37
Apr 20 '15 at 10:09
source share

Yes, you can do <property name="defaultLocale" value="#{ systemProperties['user.region']}"/> for example.

The systemProperties variable is predefined, see 6.4.1 XML-based configuration .

+25
Oct 19 '10 at 5:59
source share

In the bean definition, be sure to include "searchSystemEnvironment" and set it to true. And if you use it to create a file path, specify it as a file: /// url.

So for example, if you have a configuration file located in

 /testapp/config/my.app.config.properties 

then set the environment variable as follows:

 MY_ENV_VAR_PATH=/testapp/config 

and your application can upload the file using the bean definition as follows:

eg.

 <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="searchSystemEnvironment" value="true" /> <property name="searchContextAttributes" value="true" /> <property name="contextOverride" value="true" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>file:///${MY_ENV_VAR_PATH}/my.app.config.properties</value> </list> </property> </bean> 
+8
Nov 06
source share

Using Spring EL, you can write an eis example as follows

 <bean id="myBean" class="path.to.my.BeanClass"> <!-- can be overridden with -Dtest.target.host=http://whatever.com --> <constructor-arg value="#{systemProperties['test.target.host'] ?: 'http://localhost:18888'}"/> </bean> 
+7
Mar 25 '14 at 9:53
source share

In my use case, I needed to access only the system properties, but provide default values ​​if they are undefined.

Here's how you do it:

 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="searchSystemEnvironment" value="true" /> </bean> <bean id="myBean" class="path.to.my.BeanClass"> <!-- can be overridden with -Dtest.target.host=http://whatever.com --> <constructor-arg value="${test.target.host:http://localhost:18888}"/> </bean> 
+5
Sep 10 '13 at 16:20
source share

Announce the placement owner as follows

 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="locations"> <list> <value>file:///path.to.your.app.config.properties</value> </list> </property> </bean> 

Then let's say you want to read System.property("java.io.tmpdir") for your Tomcat bean or any bean, and then add the following to your properties file:

 tomcat.tmp.dir=${java.io.tmpdir} 
+4
Mar 22 '16 at 10:03
source share

Here's how you do it:

 <bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" scope="prototype"> <property name="targetObject" value="#{@systemProperties}" /> <property name="targetMethod" value="putAll" /> <property name="arguments"> <util:properties> <prop key="deployment.env">dev</prop> </util:properties> </property> </bean> 

But remember that spring first loads and then loads this bean MethodInvokingFactoryBean. Therefore, if you are trying to use this for your test case, make sure you use the one that depends on it. E.g. in this case

If you use it for your main class, it is better to set this property using your pom.xml, as

 <systemProperty> <name>deployment.env</name> <value>dev</value> </systemProperty> 
+1
Mar 27 '15 at 14:34
source share

You can specify your variable attributes in the properties file and define environment properties files such as local.properties, production.propertied, etc.

Now, based on the environment, one of these properties files can be read in one of the listeners called at startup, for example, ServletContextListener.

The property file will contain special values ​​for various keys.

Example "local.propeties"

 db.logsDataSource.url=jdbc:mysql://localhost:3306/logs db.logsDataSource.username=root db.logsDataSource.password=root db.dataSource.url=jdbc:mysql://localhost:3306/main db.dataSource.username=root db.dataSource.password=root 

Example "production.properties"

 db.logsDataSource.url=jdbc:mariadb://111.111.111.111:3306/logs db.logsDataSource.username=admin db.logsDataSource.password=xyzqer db.dataSource.url=jdbc:mysql://111.111.111.111:3306/carsinfo db.dataSource.username=admin db.dataSource.password=safasf@mn 

To use these property files, you can use a REsource as follows

  PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("classpath:"+System.getenv("SERVER_TYPE")+"DB.properties"); configurer.setLocation(resource); configurer.postProcessBeanFactory(beanFactory); 

SERVER_TYPE can be defined as an environment variable with corresponding values ​​for the local and production environments.

With these changes, appplicationContext.xml will have the following changes

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="${db.dataSource.url}" /> <property name="username" value="${db.dataSource.username}" /> <property name="password" value="${db.dataSource.password}" /> 

Hope this helps.

+1
Dec 18 '15 at 4:35
source share

Thanks @Yiling. It was a hint.

 <bean id="propertyConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="searchSystemEnvironment" value="true" /> <property name="locations"> <list> <value>file:#{systemEnvironment['FILE_PATH']}/first.properties</value> <value>file:#{systemEnvironment['FILE_PATH']}/second.properties</value> <value>file:#{systemEnvironment['FILE_PATH']}/third.properties</value> </list> </property> </bean> 

After that, you should have one environment variable named "FILE_PATH". Make sure you restart your terminal / IDE after creating this environment variable.

0
Mar 28 '16 at 14:14
source share

To get the value of a system variable, Simpy uses the code below:

 System.getenv("property-name"); 
-3
Oct 27 '15 at 17:32
source share



All Articles