The spring.profiles.active property tells Spring which profiles are active when the application starts. Active profiles decide which @Profile beans are created and which profile-specific configuration files, such as application-prod.yml, are loaded on top of the base application.yml.
You use it to keep one build and switch behaviour per environment: local, test, staging and production. You can set it in a file, an environment variable, a JVM option or a command-line argument.
A profile is a named set of configuration and beans. When no profile is active, Spring uses the default profile. When you activate one or more profiles, Spring does two things:
application-{profile}.properties or .yml after application.properties, and values in the profile file override the base values.@Profile that match, and skips the others.The property accepts a comma-separated list, so several profiles can be active at once: spring.profiles.active=prod,metrics.
Spring Boot reads the property from any external configuration source. In order from higher to lower precedence among the common ones:
| Source | Example |
|---|---|
| Command-line argument | java -jar app.jar --spring.profiles.active=prod |
| JVM system property | java -Dspring.profiles.active=prod -jar app.jar |
| Environment variable | SPRING_PROFILES_ACTIVE=prod |
application.properties or application.yml | spring.profiles.active=dev |
For containers and Kubernetes, the environment variable is the usual choice because it needs no rebuild.
With Maven during development you can pass it to the plugin:
With Gradle, ./gradlew bootRun --args='--spring.profiles.active=dev' does the same.
Setting dev as the default in application.yml is convenient locally, but make sure the production environment overrides it explicitly. A forgotten override is a common cause of production apps running against the wrong database.
Put shared defaults in application.yml and environment differences in separate files.
Never commit production passwords to these files. Supply secrets through environment variables or a secrets manager.
A single YAML file can hold several documents. Since Spring Boot 2.4 you target a profile with spring.config.activate.on-profile, which replaced the old spring.profiles key.
You cannot put spring.profiles.active inside a profile-specific document or file, because Spring Boot rejects it as an invalid configuration.
The @Profile annotation includes a bean or configuration class only when the profile is active.
Profile expressions support negation (!dev), AND (a & b) and OR (a | b), and they need parentheses when you mix operators.
Profile groups let one name activate several profiles. They were added in Spring Boot 2.4.
Running with --spring.profiles.active=production now activates production, prod-db and prod-messaging. To add profiles on top of those chosen externally, use spring.profiles.include, which is available in non-profile-specific files, or set spring.profiles.active through the SpringApplication API with setAdditionalProfiles.
In a test, annotate the class with @ActiveProfiles.
You can check what is active at runtime with Environment.
Spring uses the default profile, so only application.yml or application.properties and beans without a profile are loaded.
Yes. Separate them with commas, for example --spring.profiles.active=prod,metrics.
Set the SPRING_PROFILES_ACTIVE environment variable on the container. Spring Boot maps it to the property automatically.
The old spring.profiles key used to select a document was replaced by spring.config.activate.on-profile in Spring Boot 2.4. spring.profiles.active is still current.
spring.profiles.active selects the profiles that shape configuration and beans at startup. Keep shared settings in the base file, put environment differences in application-{profile} files or multi-document YAML, use @Profile for environment-specific beans, set the value through an environment variable in deployments, and keep secrets out of the files.