To implement profiles in a Spring Boot application, put shared settings in application.properties (or application.yml), add one application-{profile} file per environment, and activate a profile with spring.profiles.active, usually through an environment variable or a command-line argument. You can also bind beans to a profile with @Profile.
Profiles let the same build artifact run in development, test and production with different databases, ports, logging levels and beans. This guide covers the file layout, every common way to activate a profile, profile groups, @Profile and testing, for current Spring Boot 3.x releases.
@ProfileSpring Boot always loads application.properties or application.yml first. When a profile such as dev is active, it also loads application-dev.properties (or .yml) and lets those values override the defaults.
Keep only values that are the same everywhere in the default file:
Then put environment differences in the profile files:
The production file reads credentials from environment variables, so no secret is committed to the repository. Profile-specific values always win over the default file.
With YAML you can keep every profile in one file by separating documents with --- and tagging each one with spring.config.activate.on-profile.
The old spring.profiles: dev key inside a YAML document was removed in Spring Boot 2.4 in favour of spring.config.activate.on-profile. Many older tutorials still show the removed form, and it no longer works. The same multi-document syntax works in .properties files if you separate documents with a line containing #---.
Spring Boot reads the active profiles from several places. The first match in this list wins over the ones below it.
| Method | Example | Typical use |
|---|---|---|
| Command-line argument | --spring.profiles.active=prod | Local runs, scripts |
| JVM system property | -Dspring.profiles.active=prod | Application servers, IDE run configs |
| Environment variable | SPRING_PROFILES_ACTIVE=prod | Docker, Kubernetes, CI |
| Config file | spring.profiles.active=dev | Local default only |
| Programmatic | SpringApplication.setAdditionalProfiles("dev") | Embedded launchers |
Several profiles are separated by commas, and all of their files are loaded; the later profile overrides the earlier one when keys clash.
Setting spring.profiles.active inside application.properties is fine as a developer default, but do not rely on it for production. It cannot be set inside a profile-specific file, and an environment variable is easier to change per deployment without rebuilding.
If no profile is active, Spring uses the default profile. You can change that fallback with spring.profiles.default.
@ProfileUse @Profile on a @Component, @Configuration class or @Bean method so the bean exists only when the profile is active.
Profile expressions are supported: !dev means every profile except dev, dev | test means either one, and prod & eu requires both. Mixing & and | needs parentheses.
You can also read the active profiles at runtime through Environment:
When one environment needs several profiles, define a group so you activate a single name.
Running with --spring.profiles.active=prod now activates prod, proddb, prodmq and metrics. This keeps database, messaging and observability settings in separate files without long command lines.
Use @ActiveProfiles on a test class to select profiles for that test only.
A matching src/test/resources/application-test.properties file can point the tests at an in-memory database or a container.
application-prod.properties; read them from environment variables or a secret manager instead.spring.profiles YAML key instead of spring.config.activate.on-profile.application_dev.properties or app-dev.properties); the pattern is exactly application-{profile}.@ConditionalOnProperty for that.@Profile is always created, so two beans of the same type can clash.Set the SPRING_PROFILES_ACTIVE environment variable on the container. Spring Boot maps it to spring.profiles.active automatically through relaxed binding.
Yes. List them separated by commas, for example dev,metrics. Their files are all loaded, and later profiles override earlier ones when a key appears twice.
Spring activates the profile named default, and only application.properties plus any application-default.properties are applied. Beans annotated @Profile("default") are created in that case.
Usually the profile is not actually active, the file name is wrong, or a higher-priority source such as a command-line argument or environment variable overrides it. Print env.getActiveProfiles() or start with --debug to check.
Put shared settings in application.properties, environment differences in application-{profile} files or on-profile YAML documents, and choose the profile with SPRING_PROFILES_ACTIVE or a command-line argument. Use @Profile for beans that only belong in some environments, profile groups for bundles, and @ActiveProfiles in tests. Keep secrets out of the files.