Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

 1. Overview

In this tutorial, we'll explain how to use environment variables in Spring Boot's application.properties. Then we'll demonstrate how to refer to those properties in the code.

2. Use Environment Variables in the application.properties 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 application.properties, we need to surround it with braces:

java.home=${JAVA_HOME}

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

environment.name=${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 explain 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:

@Value("${baeldung.presentation}")
private String baeldungPresentation;

3.2. Get It From Spring Environment

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

@Autowired
private Environment environment;

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

environment.getProperty("baeldung.presentation")

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:

@Component
@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:

@Autowired
private BaeldungProperties baeldungProperties;

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

baeldungProperties.getPresentation()

4. Conclusion

In this article, we learned how to define properties with different values depending on the environment, and use them in the code.

As always, the code is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!