Home  >  Article  >  Java  >  How to Access System Environment Variables in Spring Application Context?

How to Access System Environment Variables in Spring Application Context?

Susan Sarandon
Susan SarandonOriginal
2024-10-27 08:51:02789browse

How to Access System Environment Variables in Spring Application Context?

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:

  • Utilize the ${} syntax for SpEL expressions in the application context.
  • Access the systemProperties map to retrieve system environment variables. For example, to access the env variable:
#{systemProperties.env}
  • Integrate this expression into the property location attribute:
<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn