Home > Java > javaTutorial > body text

How to Access System Environment Variables in Spring Application Context?

Susan Sarandon
Release: 2024-10-27 08:51:02
Original
688 people have browsed it

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}
Copy after login
  • Integrate this expression into the property location attribute:
<code class="xml"><util:properties id="dbProperties" location="classpath:config_#{systemProperties.env}/db.properties" /></code>
Copy after login

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']}
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!