Using spring.profiles.include for Flexible Environment Configuration in Spring Boot
In real-world deployments, Spring Boot applications often run across multiple environments such as development, testing, and production. Each environment typically requires distinct settings—like database URLs, port numbers, or external service endpoints. Manually editing configuraton files per environment before packaging is error-prone and inefficient.
Spring Boot offers an elegant solution for environment-specific configuration through profile-based property files. These files follow the naming convention application-{profile}.properties, where {profile} identifies the environment—e.g.,
application-dev.propertiesfor development,application-test.propertiesfor testing,application-prod.propertiesfor production.
A specific profile is activated via the spring.profiles.active property. For instance, setting spring.profiles.active=test in application.properties loads application-test.properties.
Core Mechanism: Profile-Specific Properties
To illustrate, suppose each environment uses a unique server port:
application-dev.properties:server.port=8080application-test.properties:server.port=9090application-prod.properties:server.port=80
The default profile is set in application.properties as:
spring.profiles.active=dev
When launching the application:
java -jar app.jar→ Port 8080 (dev environment)java -jar app.jar --spring.profiles.active=test→ Port 9090java -jar app.jar --spring.profiles.active=prod→ Port 80
In CentOS, a typical deployment command might be:
java -jar -Dloader.path=./lib \
-Dspring.profiles.active=test \
-Xms128m -Xmx512m app.jar
Note the precedence order for profile activation: Command-line argument > -D JVM flag > System environment variables > Static config files
Activating Multiple Profiles
Multiple profiles can be active simultaneously by separating them with commas:
--spring.profiles.active=dev,test
This merges configurations, with later profiles overriding earlier ones where conflicts occur.
spring.profiles.include for Modular Overlays
Sometimes, you need reusable shared configuration layers—for example, common logging or monitoring settings applied across all environment, in addition to the active profile. The spring.profiles.include directive enables this:
In YAML format (application.yml):
spring:
profiles:
include:
- logging
- monitoring
Or comma-separated:
spring:
profiles:
include: logging,monitoring
This implicitly activates the logging and monitoring profiles, loading application-logging.yml and application-monitoring.yml, which are then combined with the active profile’s settings.
⚠️ Spring Boot 2.4+ Compatibility NoteIn Spring Boot 2.4 and latter, the legacy processing model for profiles was deprecated. To retain the above behavior using
spring.profiles.include, either:
Enable legacy mode:
spring: config: use-legacy-processing: trueOr adopt the newer
spring.config.activate.on-profilesyntax (recommended for new projects):spring: config: activate: on-profile: dev