Reading System Environment Variables in Spring Application Context
In Spring, accessing system environment variables within the application context can be achieved using Spring Expression Language (SpEL) introduced in Spring 3.0. To do so, follow these steps:
#{systemProperties.env}
<code class="xml"><util:properties id="dbProperties" location="classpath:config_#{systemProperties.env}/db.properties" /></code>
By running this code with -Denv=QA, you can change the property location dynamically based on the system environment variable.
Accessing OS-Level Variables
To access system environment variables at the OS level (instead of those set within the JVM):
#{systemEnvironment['ENV_VARIABLE_NAME']}
For instance, to access the ENV_VARIABLE_NAME environment variable:
<code class="xml"><util:properties id="dbProperties" location="classpath:config_#{systemEnvironment.ENV_VARIABLE_NAME}/db.properties" /></code>
The above is the detailed content of How to Access System Environment Variables in Spring Application Context?. For more information, please follow other related articles on the PHP Chinese website!