A profile in Spring Boot is a named group of configuration and beans that you activate for a specific environment, such as dev, test or prod. Profiles let one build artifact behave differently per environment, for example using an in-memory database locally and PostgreSQL in production, without code changes.
You activate profiles with the spring.profiles.active property, an environment variable or a command-line argument, and you mark beans with @Profile. The examples target Spring Boot 3.x.
Spring Boot loads application.properties or application.yml first, then overlays application-{profile} files for each active profile. Values in the profile file override the defaults.
With a single multi-document YAML file you can use spring.config.activate.on-profile in each document instead. Note that spring.profiles was replaced by this property in Spring Boot 2.4.
Pick whichever method suits the environment:
| Method | Example |
|---|---|
| Property file | spring.profiles.active=dev |
| Command line | java -jar app.jar --spring.profiles.active=prod |
| Environment variable | SPRING_PROFILES_ACTIVE=prod |
| JVM option | -Dspring.profiles.active=prod |
| Maven plugin | mvn spring-boot:run -Dspring-boot.run.profiles=dev |
| Tests | @ActiveProfiles("test") |
If nothing is set, Spring uses the default profile. You can activate several profiles with a comma-separated list. Do not hard-code prod in a file that ships in the artifact; set it outside, in the environment.
@Profile includes a bean or configuration class only when a profile is active.
Profile expressions support ! for not, & for and, and | for or, and parentheses are required when mixing operators, for example @Profile("(dev | test) & !ci").
Groups, available since Spring Boot 2.4, let one profile name activate several others:
Running with production now activates production, proddb and prodmq, each loading its own application-* file.
The test profile files, such as application-test.yml, are loaded for that test class only. You can check the active profiles at runtime through Environment.getActiveProfiles().
The following 1 profile is active: "prod".If no profile is active, Spring uses default. Beans annotated with @Profile("default") are created only in that case.
Yes. Use a comma-separated list, for example --spring.profiles.active=prod,metrics. Later profiles override earlier ones.
Set the SPRING_PROFILES_ACTIVE environment variable on the container.
spring.profiles.active and spring.profiles.include?active selects the profiles. include adds profiles unconditionally to whatever is active, but it cannot be used inside profile-specific files since Spring Boot 2.4.
Spring Boot profiles let you vary configuration and beans per environment. Use application-{profile} files, activate them from outside the artifact with SPRING_PROFILES_ACTIVE, mark environment-specific beans with @Profile, and group related profiles with spring.profiles.group.