Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject environmental variables inside spring xml configuration?

AWS talks about System.getProperty("JDBC_CONNECTION_STRING") in http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html after we set up our environmental variables. All great except I can't call System.getProperty inside my Spring XML configuration code nor I can call for resource bundle shortcuts since resources bundle itself has to extract somehow these environmental variables to serve them. Could you kindly help me please to convert this example config to use environmental variables ? :-)

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName" value="com.mysql.jdbc.Driver" />     <property name="url" value="jdbc:mysql://secrethost:007/whois?autoReconnect=true" />     <property name="username" value="bond" />     <property name="password" value="abuginsidemistycorner" />     <property name="initialSize" value="100" />      <property name="minEvictableIdleTimeMillis">         <value>300000</value>     </property>      <property name="timeBetweenEvictionRunsMillis">         <value>60000</value>     </property>      <property name="maxIdle" value="20" /> </bean> 

I was not able to understand what do people do here:

Can I use an Environment variable based location for Spring FileSystemResource? which would work for recent spring version?

like image 270
Aubergine Avatar asked Sep 11 '13 14:09

Aubergine


People also ask

Can you use environment variables in XML?

Answer. Yes, you can set and use environment variables and use system environment variables, but you must enclose environment variable references in brackets "{}" in deployment. xml.

How do I set environment specific properties in Spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.

What 2 types of file formats can use to inject properties into the Spring environment object?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.


1 Answers

First add a <context:property-placeholder .. /> element to your configuration.

<context:property-placeholder /> 

Then simply use placeholders in your config.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName" value="com.mysql.jdbc.Driver" />     <property name="url" value="${JDBC_CONNECTION_STRING}" />     <property name="username" value="bond" />     <property name="password" value="abuginsidemistycorner" />     <property name="initialSize" value="100" />     <property name="minEvictableIdleTimeMillis" value="30000" />     <property name="timeBetweenEvictionRunsMillis" value="60000" />     <property name="maxIdle" value="20" /> </bean> 

Make sure that the placeholder names match your variables you have setup.

like image 56
M. Deinum Avatar answered Sep 19 '22 15:09

M. Deinum