Home > Java > javaTutorial > body text

How to Read System Environment Variables in Spring Application Context?

Linda Hamilton
Release: 2024-10-29 03:05:02
Original
300 people have browsed it

How to Read System Environment Variables in Spring Application Context?

Reading System Environment Variables in Spring Application Context

In Spring applications, there may be a need to read and use environment variables from the operating system. This is especially useful when managing application configurations based on different environments, such as development, QA, and production.

To read a system environment variable in Spring 3.0, the Spring Expression Language (SpEL) can be utilized. Here's how it can be achieved:

<code class="xml"><util:properties id="dbProperties" 
    location="classpath:config_#{systemProperties['env']}/db.properties" /></code>
Copy after login

In this configuration, the property file to be loaded is dynamically selected based on the value of the env system environment variable. When starting the application, you can specify the environment using the -D command-line argument, such as:

<code class="bash">java -Denv=QA ...</code>
Copy after login

Note: For accessing OS-level environment variables directly, you can use systemEnvironment instead of systemProperties in the SpEL expression, like:

<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 Read 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