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.
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.comIn 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-mqGrouping 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.comStarting the application now shows the active groups:
The following profiles are active: dev,dev-db,dev-mqSwitching to the production environment by setting spring.profiles.active: "prod" yields:
The following profiles are active: prod,prod-db,prod-mqIn 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
