This post will discuss how to read the system environment variable (OS-level variables) in a Spring Boot application.

When Spring Boot initializes its environment, it uses properties files, YAML files, environment variables, and command-line arguments to externalize the configuration. This post will discuss how to read the system environment variable.

1. Spring’s Environment Interface

The property values can be accessed through Spring’s Environment abstraction, as shown below:

 
Another alternative is to simply inject Environment into our controller/bean.

2. @Value annotation

We can also use the @Value annotation to load variables from the application.properties.

Suppose in application.properties, we have a property spring.datasource.url. Then the property values can be injected directly into your beans by using the @Value annotation, as shown below:

That’s all about reading the System environment variable in Spring Boot.