Spring Boot 2.4 Multi-Profile Configuration: From spring.profiles to spring.config.activate.on-profile
This article explains how Spring Boot 2.4 changes the way multi‑environment configurations are defined and activated, replacing the old spring.profiles property with spring.config.activate.on-profile and showing how to specify and launch profiles both in YAML files and via command‑line arguments.
In the latest Spring Boot 2.4, the configuration loading mechanism has been significantly changed. This article explains the differences in multi‑environment configuration between previous versions and 2.4.
Multi‑environment configuration
Before 2.4
Previously we defined different profiles in YAML using spring.profiles, for example:
spring:
profiles: "dev"
name: dev.didispace.com
---
spring:
profiles: "test"
name: test.didispace.com
---
spring:
profiles: "prod"
name: prod.didispace.comAfter 2.4
From 2.4 onward the spring.profiles property is replaced by spring.config.activate.on-profile. The same configurations become:
spring:
config:
activate:
on-profile: "dev"
name: dev.didispace.com
---
spring:
config:
activate:
on-profile: "test"
name: test.didispace.com
---
spring:
config:
activate:
on-profile: "prod"
name: prod.didispace.comActivating a profile at startup
When launching the application you still set the active profile with spring.profiles.active, whose value must match the name defined by spring.config.activate.on-profile. For example:
java -jar myapp.jar -Dspring.profiles.active=devThe startup log will show the activated profile:
2020-12-16 16:34:20.614 INFO 5951 --- [ main] c.d.chapter12.Chapter12Application : The following profiles are active: devYou can also set spring.profiles.active directly in application.yml to define a default profile, typically “dev” for local development, while production environments are usually activated via command‑line arguments.
Code example
The related example code is available in the chapter1-2 directory of the repository:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/didispace/SpringBoot-Learning/
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.
