eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

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

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

 1. Overview

In this tutorial, we’ll discuss how to use environment variables in Spring Boot’s application.properties 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 application.properties 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

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

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

@Value("${baeldung.presentation}")
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:

@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. Use Environment Variable in the application.yml File

Just like application.properties, 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:

spring:
  datasource:
    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 application.properties. We can inject the value using the @Value annotation. Also, we can use the Environment class. Finally, we can use the @ConfigurationProperties annotation.

5. Loading Multiple Properties From an Environment Variable

Starting with Spring Boot 3.5.0-M2, we can now load multiple configuration properties from a single environment variable. Traditionally, we often define separate environment variables for each property. Now,  we can consolidate related properties into a single environment variable.

For example, let’s define an environment variable named DATABASE_CONFIG:

export DATABASE_CONFIG="
DATABASE_URL=jdbc:h2:mem:testdb 
USERNAME=sa
PASSWORD=password
"

The format above mirrors the key-value pair structure used in application.properties files.

Alternatively, we can define the properties in the environment variable using the YAML format:

export DATABASE_CONFIG=$(cat <<EOF
DATABASE_URL: jdbc:h2:mem:testdb
USERNAME: sa
PASSWORD: password
EOF
)

Spring Boot supports both the properties format and the YAML format content within an environment variable when using the spring.config.import property.

Next, let’s reference the environment variable in the application.properties file by using the spring.config.import property:

spring.config.import=env:DATABASE_CONFIG

Here, we use the env keyword with the environment variable name, prompting Spring Boot to load properties from the specified environment variable.

Finally, let’s use some of the properties in our database configuration as a placeholder:

spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${USERNAME}
spring.datasource.password=${PASSWORD}

In the code above, we use the properties defined in the DATABASE_CONFIG environment, avoiding defining each property as a separate environment variable.

6. 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 application.properties and application.yml files. Finally, we looked at examples of injecting the defined properties into the example code.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

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

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)