Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


 1. Overview

In this tutorial, we’ll discuss how to use environment variables in Spring Boot’s and application.yml. Then, we’ll learn how to refer to those properties in the code.

Further reading:

Properties with Spring and Spring Boot

Tutorial for how to work with properties files and property values in Spring.

Using application.yml vs in Spring Boot

Spring Boot supports both .properties and YAML. We explore the differences between injecting properties, and how to provide multiple configurations.

Environment Variable Prefixes in Spring Boot 2.5

Learn how to use a prefix for environment variables with Spring Boot.

2. Use Environment Variables in the File

Let’s define a global environment variable called JAVA_HOME with the value “C:\Program Files\Java\jdk-11.0.14“.

To use this variable in Spring Boot’s, we need to surround it with braces:


We can also use the system properties in the same way. For instance, on Windows, an OS property is defined by default:${OS}

It’s also possible to combine several variable values. Let’s define another environment variable, HELLO_BAELDUNG, with the value “Hello Baeldung“. We can now concatenate our two variables:

baeldung.presentation=${HELLO_BAELDUNG}. Java is installed in the folder: ${JAVA_HOME}

The property baeldung.presentation now contains the following text: “Hello Baeldung. Java is installed in the folder: C:\Program Files\Java\jdk-11.0.14“.

This way, our properties have different values depending on the environment.

3. Use Our Environment-Specific Properties in Code

Given that we start a Spring context, we’ll now see how we can inject the property value into our code.

3.1. Inject the Value With @Value

First, we can use the @Value annotation. @Value handles setter, constructor, and field injections:

private String baeldungPresentation;

3.2. Get It From the Spring Environment

We can also get the value of the property via Spring’s Environment. We’ll need to autowire it:

private Environment environment;

The property value can now be retrieved, thanks to the getProperty() method:


3.3. Group Properties With @ConfigurationProperties

The @ConfigurationProperties annotation is very useful if we want to group properties together. We’ll define a Component that will gather all properties with a given prefix, in our case baeldung. Then, we can define a setter for each property. The name of the setter is the rest of the name of the property. In our case, we have only one, called presentation:

@ConfigurationProperties(prefix = "baeldung")
public class BaeldungProperties {

    private String presentation;

    public String getPresentation() {
        return presentation;

    public void setPresentation(String presentation) {
        this.presentation = presentation;

We can now autowire a BaeldungProperties object:

private BaeldungProperties baeldungProperties;

Finally, to get the value of a specific property, we need to use the corresponding getter:


4. Use Environment Variable in application.yml File

Just like, application.yml is a configuration file that defines various properties and settings for an application. To use an environment variable, we need to declare its name in the property placeholder.

Let’s see an example application.yml file with a property placeholder and the variable name:

    url: ${DATABASE_URL}

The example above shows we’re trying to import a database URL inside our Spring Boot application. The ${DATABASE_URL} expression prompts Spring Boot to look for an environment variable with the name DATABASE_URL.

To define an environment variable in application.yml, we must start with a dollar sign, followed by an opening curly brace, the name of the environment variable, and a closing curly brace. All these combined make up the property placeholder and the environment variable name.

Furthermore, we can use the environment-specific property in our code, just like we do with We can inject the value using the @Value annotation. Also, we can use the Environment class. Finally, we can use the @ConfigurationProperties annotation.

5. Conclusion

In this article, we learned how to define properties with different values depending on the environment and use them in the code. Additionally, we saw how to define environment variables in and application.yml files. Finally, we looked at examples of injecting the defined properties into the example code.

As always, the code is available on GitHub.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!