Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you implement profiles in a Spring Boot application?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJan 21, 2025
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • How profile-specific configuration files work
  • Defining profiles in a single YAML file
  • Ways to activate a profile
  • Binding beans to a profile with @Profile
  • Profile groups
  • Testing with profiles
  • Common mistakes to avoid
  • Frequently Asked Questions
    • How do I set the active profile in Docker or Kubernetes?
    • Can more than one profile be active at the same time?
    • What happens if no profile is set?
    • Why is my profile-specific value ignored?
  • Summary

How profile-specific configuration files work

Spring 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.

text

Keep only values that are the same everywhere in the default file:

properties

Then put environment differences in the profile files:

properties
properties

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.

Defining profiles in a single YAML 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.

yaml

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 #---.

Ways to activate a profile

Spring Boot reads the active profiles from several places. The first match in this list wins over the ones below it.

MethodExampleTypical use
Command-line argument--spring.profiles.active=prodLocal runs, scripts
JVM system property-Dspring.profiles.active=prodApplication servers, IDE run configs
Environment variableSPRING_PROFILES_ACTIVE=prodDocker, Kubernetes, CI
Config filespring.profiles.active=devLocal default only
ProgrammaticSpringApplication.setAdditionalProfiles("dev")Embedded launchers
bash

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.

Binding beans to a profile with @Profile

Use @Profile on a @Component, @Configuration class or @Bean method so the bean exists only when the profile is active.

java

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:

java

Profile groups

When one environment needs several profiles, define a group so you activate a single name.

properties

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.

Testing with profiles

Use @ActiveProfiles on a test class to select profiles for that test only.

java

A matching src/test/resources/application-test.properties file can point the tests at an in-memory database or a container.

Common mistakes to avoid

  • Committing real passwords to application-prod.properties; read them from environment variables or a secret manager instead.
  • Using the removed spring.profiles YAML key instead of spring.config.activate.on-profile.
  • Misnaming a file (application_dev.properties or app-dev.properties); the pattern is exactly application-{profile}.
  • Overusing profiles for feature toggles; use ordinary properties or @ConditionalOnProperty for that.
  • Forgetting that a bean with no @Profile is always created, so two beans of the same type can clash.

Frequently Asked Questions

How do I set the active profile in Docker or Kubernetes?

Set the SPRING_PROFILES_ACTIVE environment variable on the container. Spring Boot maps it to spring.profiles.active automatically through relaxed binding.

Can more than one profile be active at the same time?

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.

What happens if no profile is set?

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.

Why is my profile-specific value ignored?

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.

Summary

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.

Share