Master Spring Boot 2.4 Multi‑Environment Config: From Profiles to Groups

This article explains how Spring Boot 2.4 changes multi‑environment configuration by replacing the old spring.profiles.include grouping with the new spring.profiles.group mechanism, showing before‑and‑after YAML examples, activation logs, and practical steps to migrate existing projects.

Programmer DD
Programmer DD
Programmer DD
Master Spring Boot 2.4 Multi‑Environment Config: From Profiles to Groups

Earlier this week an article introduced Spring Boot 2.4's support for multi‑environment configuration; this follow‑up dives into the additional changes introduced in version 2.4.

Understanding spring.profiles.include

The property spring.profiles.include is used to import other configuration groups, typically to simplify configuration when many middleware components such as MySQL, Redis, or MQ are involved.

Grouping configuration before 2.4

Prior to 2.4, developers grouped configurations like this:

spring:
  profiles:
    active: "dev"
---
spring.profiles: "dev"
spring.profiles.include: "dev-db,dev-mq"
---
spring.profiles: "dev-db"

db: dev-db.didispace.com
---
spring.profiles: "dev-mq"

mq: dev-mq.didispace.com

In this setup:

The first profile spring.profiles.active: dev activates the default dev configuration.

The dev configuration includes dev-db and dev-mq via spring.profiles.include, loading separate YAML sections for each middleware.

Running the application with Spring Boot 2.3 or earlier prints:

The following profiles are active: dev,dev-db,dev-mq

Grouping configuration in Spring Boot 2.4

After upgrading to Spring Boot 2.4, the previous approach no longer works. Even if spring.profiles is replaced with spring.config.activate.on-profile, the additional groups are not activated. Instead, the new spring.profiles.group key must be used.

Example configuration:

spring:
  profiles:
    active: "dev"
    group:
      "dev": "dev-db,dev-mq"
      "prod": "prod-db,prod-mq"
---
spring:
  config:
    activate:
      on-profile: "dev-db"

db: dev-db.didispace.com
---
spring:
  config:
    activate:
      on-profile: "dev-mq"

mq: dev-mq.didispace.com
---
spring:
  config:
    activate:
      on-profile: "prod-db"

db: prod-db.didispace.com
---
spring:
  config:
    activate:
      on-profile: "prod-mq"

mq: prod-mq.didispace.com

Starting the application now shows the active groups:

The following profiles are active: dev,dev-db,dev-mq

Switching to the production environment by setting spring.profiles.active: "prod" yields:

The following profiles are active: prod,prod-db,prod-mq

In the 2.4 format, all environment definitions reside in the default configuration under spring.profiles.group. The key (e.g., dev or prod) represents the environment, while the value lists the profile groups to load.

Code examples

Complete examples are available in the chapter1-3 directory of the following repositories:

GitHub: https://github.com/dyc87112/SpringBoot-Learning/

Gitee: https://gitee.com/didispace/SpringBoot-Learning/

If you find this tutorial helpful, consider starring the repository.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendConfigurationSpring BootYAMLMulti-EnvironmentProfiles
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.