How to use Spring Boot profiles?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot profiles let you switch configuration and beans based on the environment your application is running in. That is useful for things like development databases, production-only integrations, or local debugging features. The core idea is simple: keep common settings in the base config, then layer profile-specific overrides on top.
Put Shared and Environment-Specific Config in Separate Files
The most common pattern is:
- '
application.ymlfor shared defaults' - '
application-dev.ymlfor development overrides' - '
application-prod.ymlfor production overrides'
Example base configuration:
Example development override in application-dev.yml:
Example production override in application-prod.yml:
When dev is active, Boot loads the base file and then overrides matching values with those from application-dev.yml.
Activate Profiles
The current profile can be activated in several ways.
At the command line:
With an environment variable:
In tests:
You can also set spring.profiles.active in configuration, but that is usually best reserved for local defaults rather than deployment-controlled environments.
Load Beans Only for Certain Profiles
Profiles are not just for properties. They also control bean registration with @Profile.
That keeps environment-specific wiring out of your business logic.
Use spring.config.activate.on-profile for YAML Sections
Instead of separate files, you can also put multiple YAML documents in one file and activate sections by profile.
This is the current style for profile-specific YAML documents. It is clearer than older profile markers and matches modern Boot configuration handling.
Use Profile Groups for Bundles of Behavior
If your application has related settings that should always travel together, profile groups can help.
Now activating staging also activates prod and metrics. This can reduce duplication when environments share most behavior with just a few extra features layered on.
Profiles also interact with normal property precedence. Command-line arguments, environment variables, and external config can still override values inside profile files. That is useful in deployments because you can keep environment shape in profiles while leaving last-mile differences to runtime configuration.
Common Pitfalls
The first mistake is putting every environment difference into profiles, including secrets that should really come from external secret management or environment variables.
Another issue is activating a profile from inside a profile-specific document. That creates confusing configuration cycles and should be avoided.
Some developers also overuse @Profile for business decisions that belong in normal application configuration. Profiles are best for environment shape, not for everyday runtime branching.
Finally, remember that multiple profiles can be active at once. If two active profiles define the same property, override order matters, so keep the configuration model deliberate rather than accidental.
Summary
- Spring Boot profiles let you layer environment-specific configuration and beans on top of shared defaults.
- The common pattern is
application.ymlplus files likeapplication-dev.ymlandapplication-prod.yml. - Activate profiles with CLI args, environment variables, or
@ActiveProfilesin tests. - Use
@Profilefor environment-specific bean registration. - Prefer
spring.config.activate.on-profilefor profile-specific YAML sections in current Boot configuration.
Related reading
- How to use Spring Boot with MySQL database and JPA?
- How to use Spring managed Hibernate interceptors in Spring Boot?
- How to use StringBuilder wisely?
- How to use ThreeTenABP in Android Project
- How to use UTF-8 in resource properties with ResourceBundle
- How to use wait and notify in Java without IllegalMonitorStateException?
- How to use WeakReference in Java and Android development?
- How to use WebClient to execute synchronous request?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.