Master Spring Boot Profiles: Seamlessly Switch Environments Without Rebuilding
This article explains how Spring Boot's profile mechanism lets developers maintain separate configuration files for development, testing, and production environments, showing both properties and YAML examples, command‑line activation, and a deep dive into the underlying source code that processes these profiles.
Profile Purpose
Spring Boot provides a Profile feature that allows the application to load different configuration files based on the active environment, eliminating the need to manually edit files or rebuild the package for each environment.
Basic Profile Usage
Typical environments include development, testing, and production, each requiring its own configuration. Spring Boot can manage these with four files:
application.properties – common configuration
application-dev.properties – development configuration
application-test.properties – testing configuration
application-prod.properties – production configuration
In application.properties set the active profile: spring.profiles.active=prod The value "prod" corresponds to application-prod.properties. Spring Boot reads the common file, appends the profile name, and loads the specific file.
Example: use port 8080 for development and 18080 for production.
# application-dev.properties
server.port=8080 # application-prod.properties
server.port=18080You can also activate a profile via the command line:
java -jar springboot.jar --spring.profiles.active=prodYAML Configuration
When using YAML, all profiles can be defined in a single file:
spring:
profiles:
active: prod
server:
port: 18080
---
spring:
profiles: dev
server:
port: 8080
---
spring:
profiles: test
server:
port: 8081The first section is the default profile; you can still override it with command‑line arguments.
Source Code Analysis
During startup Spring Boot executes SpringApplication.run(...), which creates an ApplicationArguments instance and calls prepareEnvironment. This method triggers environmentPrepared events that are listened to by EventPublishingRunListener.
public ConfigurableApplicationContext run(String... args) {
// ...
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// ...
}The ConfigFileApplicationListener processes these events, loading property sources based on the active profile. It uses PropertySourceLoader implementations defined in spring.factories:
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader PropertiesPropertySourceLoadersupports .properties and .xml files, while YamlPropertySourceLoader handles .yml and .yaml. Their load methods parse the files and add them to the environment.
public class PropertiesPropertySourceLoader implements PropertySourceLoader {
public String[] getFileExtensions() { return new String[]{"properties","xml"}; }
public List<PropertySource<?>> load(String name, Resource resource) throws IOException { ... }
} public class YamlPropertySourceLoader implements PropertySourceLoader {
public String[] getFileExtensions() { return new String[]{"yml","yaml"}; }
public List<PropertySource<?>> load(String name, Resource resource) throws IOException { ... }
}The listener also assembles profile‑specific file names (e.g., application-prod.properties) and loads them in order, allowing later files to override earlier ones.
For a complete understanding, you can debug ConfigFileApplicationListener and its internal Loader class, which implements the detailed logic for file discovery, profile handling, and property source registration.
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.
