Spring Boot profiles provide a powerful mechanism to segregate application configuration for different environments, such as development, testing, and production. Mastering how to activate a specific profile is essential for any developer looking to manage environment-specific beans, properties, and logic without changing code. This guide explores every standard method to set the active profile in Spring Boot, ranging from command-line arguments and environment variables to programmatic approaches and build-tool configurations.
Understanding Spring Boot Profiles
Before diving into activation methods, it — worth paying attention to. In practice, a profile is a named logical group of configuration that is only loaded when that profile is active. properties, or application-test.Typically, developers create files like application-dev.That's why yml, application-prod. yaml alongside the main application.yml. When the dev profile is active, Spring Boot loads both the main configuration and the dev-specific overrides, with the profile-specific files taking precedence Not complicated — just consistent. Simple as that..
Profiles are annotated using @Profile("profile-name") on @Component or @Configuration classes, allowing you to swap entire implementations—such as switching between an in-memory H2 database for development and a PostgreSQL instance for production—simply by changing the active profile Not complicated — just consistent..
Setting Active Profiles via Command Line Arguments
The most direct and commonly used method for overriding the active profile during application startup is through command-line arguments. Spring Boot treats command-line arguments as property sources with the highest precedence, meaning they override values found in configuration files or environment variables The details matter here..
To activate a profile using the standard Spring property key, pass the spring.profiles.active argument when running the JAR file:
java -jar my-application.jar --spring.profiles.active=prod
You can also activate multiple profiles simultaneously by providing a comma-separated list. This is useful when you have modular profiles, such as a database profile for datasource config and a security profile for OAuth settings:
java -jar my-application.jar --spring.profiles.active=prod,database,security
Note: If you are using the spring-boot-maven-plugin or spring-boot-gradle-plugin to run the application directly via mvn spring-boot:run or gradle bootRun, you can pass arguments using the respective plugin syntax:
# Maven
mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"
# Gradle
./gradlew bootRun --args='--spring.profiles.active=dev'
Using Environment Variables
In containerized environments (Docker, Kubernetes) and CI/CD pipelines, environment variables are the standard for configuration injection. Spring Boot automatically maps environment variables to property keys by converting them to uppercase, replacing dots with underscores, and adding a SPRING_ prefix convention (though the relaxed binding rules are quite flexible).
To set the active profile via an environment variable, define SPRING_PROFILES_ACTIVE:
export SPRING_PROFILES_ACTIVE=production
java -jar my-application.jar
In a Dockerfile, this looks like:
ENV SPRING_PROFILES_ACTIVE=production
ENTRYPOINT ["java", "-jar", "my-application.jar"]
In a Kubernetes Deployment manifest, you would define it under the env section of the container spec:
env:
- name: SPRING_PROFILES_ACTIVE
value: "production"
This approach keeps the deployment artifact (the JAR) completely environment-agnostic, adhering to the Twelve-Factor App methodology.
Configuring Profiles in Application Properties/YAML
You can define a default active profile directly inside your main configuration file (application.yml or application.On top of that, properties). This is useful for setting a sensible fallback (usually dev) so the application runs locally without extra commands And that's really what it comes down to..
application.yml
spring:
profiles:
active: dev
application.properties
spring.profiles.active=dev
Critical Caveat: Values set in application.yml act as defaults. They are easily overridden by command-line arguments, environment variables, or config server values. Still, if you use spring.config.import to import external configuration or use Spring Cloud Config, the precedence rules can become complex. Generally, treat file-based configuration as the baseline for local development only That's the whole idea..
Programmatic Profile Activation
There are scenarios where you need to determine the active profile dynamically at runtime—perhaps based on a system property, a cloud provider metadata endpoint, or a custom logic check. You can achieve this by implementing SpringApplicationRunListener, ApplicationContextInitializer, or simply by customizing the SpringApplication builder in your main class.
The official docs gloss over this. That's a mistake.
Using SpringApplicationBuilder
The cleanest programmatic approach is modifying the SpringApplication builder before the context refreshes:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
// Logic to determine profile, e., checking an env var or system property
String profile = System.Consider this: g. getenv("MY_CUSTOM_ENV") != null ?
new SpringApplicationBuilder(MyApplication.class)
.profiles(profile)
.run(args);
}
}
Using ApplicationContextInitializer
For more complex logic, especially if you are building a library or starter used by other applications, an ApplicationContextInitializer is appropriate. Create a class implementing the interface:
public class DynamicProfileInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.Consider this: exists(Paths. getEnvironment();
// Example: Activate 'docker' profile if running inside a container
if (Files.That said, get("/. dockerenv"))) {
environment.
Register it in `META-INF/spring.Because of that, factories` (Spring Boot 2) or `META-INF/spring/org. springframework.context.
```properties
org.springframework.context.ApplicationContextInitializer=com.example.DynamicProfileInitializer
Profile Groups (Spring Boot 2.4+)
Starting with Spring Boot 2.This simplifies activation commands significantly. Plus, 4, Profile Groups allow you to define a logical name that expands into multiple physical profiles. Instead of remembering to activate prod,db-mysql,metrics-prometheus, you simply activate production.
Define groups in application.yml:
spring:
profiles:
group:
"production": "prod,db-mysql,metrics-prometheus,security-oauth"
"development": "dev,db-h2,metrics-none"
Now, activating the group is transparent:
java -jar app.jar --spring.Plus, profiles. In practice, active=production
Spring Boot will automatically expand production into the four listed profiles. This feature greatly reduces cognitive load and command-line length in complex microservice architectures.
Using spring.profiles.include for Additive Profiles
While spring.profiles.That said, active replaces the default profiles, spring. Think about it: profiles. include adds to them. This is useful when you have a base profile (like dev) that should always be active, but you want to optionally layer on extra features (like debug-logging or swagger-ui) without losing the base configuration.
In application.yml:
spring:
profiles:
active: dev
include:
- common-datasource
- dev-tools
If you then start the app with --spring.profiles.active=prod, the active profiles become prod, common-datasource, dev-tools. Note that include profiles are only added if the profile defining them is active. If you switch entirely away from dev to prod, the include directive inside the dev profile file won't trigger unless you also define it in application-prod.yml or the main file.
IDE-Specific Configuration
During daily development, you rarely want to type command-line arguments