Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

How do Spring profiles work, and how do you activate them in Spring Boot?

A profile is a named switch: beans annotated with @Profile and properties in files such as application-prod.yml apply only when that profile is active. In Spring Boot we activate profiles with spring.profiles.active, set as a property, an environment variable SPRING_PROFILES_ACTIVE or a command-line argument. Profile expressions such as @Profile("prod & !eu") combine profiles, spring.profiles.group lets one profile activate several others, and the default profile applies when none is active.

Why this question is asked

Profiles are how a Spring application behaves slightly differently on a laptop, in a test and in production without separate builds. Almost every Spring Boot codebase uses them, and almost every team has at least one surprise with them, such as a bean that exists locally but not in production. Interviewers ask this to check that we know the two things profiles control, how they are switched on, and the extra features beyond a single name. In this answer, we will recap what a profile is, see how it controls beans and properties, look at the ways to activate it, then cover expressions, groups and the default profile.

1A Quick Recap of Profiles

A profile is just a name, such as local, test or prod, that can be active or inactive in the Environment, the object that holds an application's configuration. Several profiles can be active at once. Spring uses the set of active profiles in two places: to decide which beans to register, and to decide which property files to load. Nothing else is special about a profile; it is a label that parts of the configuration can depend on.

2How Profiles Control Beans and Properties

For beans, we put @Profile on a @Component class, a @Configuration class or a @Bean method. Spring registers that bean only when the profile condition is true. A common use is replacing a real integration with a fake one for local development.

Java
interface EmailSender { void send(Email email); }
@Component
@Profile("!local") // every profile except local
class SmtpEmailSender implements EmailSender { ... }
@Component
@Profile("local")
class LoggingEmailSender implements EmailSender {
public void send(Email email) { System.out.println("Would send: " + email.subject()); }
}
// with spring.profiles.active=local
emailSender.send(new Email("Welcome")); // Would send: Welcome
What spring.profiles.active=prod changes
PartWithout prodWith prod
@Profile("prod") beansNot registeredRegistered
@Profile("!prod") beansRegisteredNot registered
application-prod.ymlIgnoredLoaded, overrides application.yml
application.ymlLoadedLoaded

For properties, Spring Boot loads application-{profile}.yml in addition to application.yml for each active profile, and the profile-specific values override the plain ones. So application.yml can hold defaults and application-prod.yml only what differs in production. If several profiles are active, the one activated last wins when they set the same key.

3Activating Profiles in Spring Boot

The property spring.profiles.active lists the active profiles, separated by commas. Because it is an ordinary property, we can set it anywhere a property can come from, and the usual priority rules decide which setting wins.

Java
# in application.yml (rarely a good idea for real environments)
spring:
profiles:
active: local
$ SPRING_PROFILES_ACTIVE=prod,eu java -jar shop.jar # environment variable, typical in containers
$ java -jar shop.jar --spring.profiles.active=prod # command-line argument
$ java -Dspring.profiles.active=prod -jar shop.jar # system property
@SpringBootTest
@ActiveProfiles("test") // in tests
class OrderServiceTest { }

In containers, the environment variable is the usual choice, because the deployment sets it per environment. There is also spring.profiles.include, which adds profiles on top of the active ones, but groups, described below, are usually clearer. Notice one restriction: spring.profiles.active cannot be set inside a profile-specific file, because Spring would have to know the profiles before reading the files that depend on them.

4Profile Expressions

@Profile accepts expressions that combine profile names with ! for not, & for and, and | for or. Mixing & and | needs parentheses, so the meaning is never ambiguous.

Java
@Profile("prod & !eu") // active in prod, but not when eu is also active
@Profile("local | test") // active in either
@Profile("prod & (eu | us)") // parentheses required when mixing & and |
// active profiles: prod, eu
// "prod & !eu" -> false
// "prod & (eu | us)" -> true

Expressions are powerful, and that is a reason to keep them short. A bean whose existence depends on three profiles is hard to predict, and long expressions are usually a sign that profiles are being used for configuration values that belong in properties.

5Profile Groups and the Default Profile

A profile group lets one name stand for several profiles. We define it in application.yml with spring.profiles.group, and activating the group's name activates all its members. This lets us build environments from small building blocks.

YAML
spring:
profiles:
group:
production: "postgres, kafka, real-payments"
local: "h2, fake-payments"
$ java -jar shop.jar --spring.profiles.active=production
// active profiles: production, postgres, kafka, real-payments

Finally, when no profile is active at all, Spring treats a profile named default as active. So @Profile("default") beans and application-default.yml apply only in that case, and they stop applying as soon as any profile is activated. The name of the default profile can be changed with spring.profiles.default, which is useful to make local the default for developers while production always sets its profiles explicitly.

6Follow-up questions

What happens if two active profiles set the same property?

The profile activated later in the list wins. With spring.profiles.active=prod,eu, a key set in both application-prod.yml and application-eu.yml takes the value from application-eu.yml. Relying on this ordering is fragile, so it is better if profiles do not overlap.

Can I check which profiles are active at runtime?

Yes. Spring Boot logs them at startup in a line such as 'The following 2 profiles are active: "prod", "eu"', environment.getActiveProfiles() returns them in code, and the actuator env endpoint lists them as activeProfiles.

Should every environment have its own profile?

Not necessarily. Environment differences are mostly values, which externalised configuration handles better. Profiles are most useful for real behaviour differences, such as a fake payment provider locally, and naming them after the behaviour keeps them understandable.

7Conclusion

In this answer, we saw that a profile is a named switch that controls which beans Spring registers, through @Profile, and which property files Spring Boot loads, through application-{profile}.yml. We activate profiles with spring.profiles.active from any property source, typically SPRING_PROFILES_ACTIVE in containers or @ActiveProfiles in tests. Expressions such as prod & !eu combine profiles, spring.profiles.group builds a profile from several others, and the default profile applies only when no other profile is active.

What to say in the interviewAbout 80 seconds aloud

Open with

A profile is a named switch in the Environment, and Spring uses the active profiles in two places. For beans, Profile on a component, configuration class or Bean method registers it only when the profile condition holds, which I use for things like a logging email sender locally and the real SMTP sender everywhere else.

Then, if they want more

For properties, Spring Boot loads application-prod.yml on top of application.yml when prod is active, and the profile-specific values override the defaults. To activate profiles I set spring.profiles.active, which is an ordinary property, so it can come from the SPRING_PROFILES_ACTIVE environment variable in containers, a command-line argument, a system property, or ActiveProfiles in tests.

It cannot be set inside a profile-specific file. Profile also accepts expressions with not, and, and or, like prod and not eu, and mixing and with or needs parentheses. For composing environments, spring.profiles.group lets one name such as production activate postgres, kafka and real-payments together.

And when no profile is active, the default profile is active, so default beans and application-default.yml apply only then; spring.profiles.default can rename it. In practice I keep profiles for real behaviour differences and put environment values in external configuration.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do Spring profiles work, and how do you activate them in Spring Boot?": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/32b57e57e3/