Spring BootProfilesintermediate
Updated:

Spring Boot Profiles Interview Questions and Answers

6 min read

Profile questions Spring interviewers ask — profile-specific config files, spring.profiles.active, @Profile beans, profile groups, and the deprecated spring.profiles key.

TL;DR – Quick Answer

Spring Boot profile interviews test how you separate configuration per environment: profile-specific files like application-dev.yml, the spring.profiles.active property, @Profile on beans and config classes, and activation via environment variable, JVM arg or command line. The senior follow-ups cover profile groups (spring.profiles.group), the default profile, @ActiveProfiles in tests, and that spring.profiles inside a multi-document YAML is deprecated in favour of spring.config.activate.on-profile.

On This Page

Profiles are how one Spring Boot artifact behaves correctly in dev, test and production without editing code. Interviewers use profile questions to check whether you actually run multi-environment applications or just hardcode a database URL and hope. This set covers the profile questions asked in intermediate rounds — the file naming convention, activation methods, @Profile beans, profile groups, and the deprecated-syntax trap — each with a spoken answer and the follow-up interviewers reach for.

Why interviewers ask about profiles

Every real application runs in more than one environment, and each needs different URLs, credentials, log levels and feature toggles. Profiles are Spring Boot's answer to that, and how cleanly you use them signals whether you can ship the same build everywhere — the core of twelve-factor configuration. The questions are concrete because the mechanics are concrete: file names, property keys, and precedence rules.

Q1. What is a Spring Boot profile and why use one?

A profile is a named grouping of configuration and beans that Spring activates only in a matching environment. It lets a single build load application-dev.yml in development and application-prod.yml in production, and register different beans per environment, so environment differences stay in configuration rather than in code or in separate builds.

The value is a single deployable artifact. You do not rebuild for prod; you activate the prod profile and the right property file and beans come to life. That is what keeps dev and prod behaviour aligned and eliminates "works on my machine" configuration drift.

Interview note: Follow-up: "why not just build a separate jar per environment?" Because separate builds diverge and you test something different from what you ship. One artifact plus profiles keeps the code identical and only the configuration variable.

Q2. How does Spring Boot pick up profile-specific configuration files?

Spring Boot loads application.yml (or .properties) as the base, then overlays application-{profile}.yml for each active profile. Profile-specific values override the base, so common settings live in application.yml and only the differences go in the profile files.

The naming convention is exact: the file must be application-<profileName> with the same base name. Anything not overridden falls through to the base file, which keeps the profile files small.

# application.yml (base — shared defaults)
spring:
  application:
    name: orders-service
server:
  port: 8080

# application-prod.yml (only the prod differences)
server:
  port: 80
logging:
  level:
    root: WARN

Interview note: Trap: "if application.yml sets port 8080 and application-prod.yml sets port 80, what runs under prod?" Port 80 — the profile-specific file wins over the base for keys it defines, while undefined keys still come from the base.

Q3. What are all the ways to activate a profile?

Set spring.profiles.active. You can do it with the SPRING_PROFILES_ACTIVE environment variable, a JVM system property -Dspring.profiles.active=prod, a command-line argument --spring.profiles.active=prod, or a value in a property file. You may activate multiple profiles as a comma-separated list.

The important detail is precedence: command-line arguments and environment variables override values baked into property files, which is exactly what you want so an operator can flip the environment without touching the artifact.

# any one of these activates prod
export SPRING_PROFILES_ACTIVE=prod
java -Dspring.profiles.active=prod -jar app.jar
java -jar app.jar --spring.profiles.active=prod,monitoring

Interview note: Follow-up: "which wins if the file says dev and the command line says prod?" The command line — externalized configuration overrides packaged configuration. This precedence is the whole reason profiles are operable in production.

Q4. What does @Profile do and how do you use negation?

@Profile on a bean method or @Configuration class makes that bean register only when the named profile is active. It accepts a single profile, a list (any match), and expressions including negation @Profile("!prod") and logical operators. It is how you swap one interface's implementation per environment.

The classic use is a real implementation for production and a stub for local development: a live payment gateway under prod, a fake one otherwise, both implementing the same interface so the rest of the code never changes.

@Configuration
class MailConfig {
    @Bean
    @Profile("prod")
    MailSender realMailSender() { return new SmtpMailSender(); }

    @Bean
    @Profile("!prod")
    MailSender noopMailSender() { return new LoggingMailSender(); }
}

Interview note: Trap: "what if no profile is active and a bean is @Profile(\"prod\")?" It is not registered, and if something requires that bean the context fails to start. That failure is usually a missing spring.profiles.active.

Q5. What is the default profile?

When no profile is explicitly active, Spring uses the default profile. Beans annotated @Profile("default") and an application-default.yml file apply only when nothing else is active. Once you activate any profile, default is no longer applied.

This matters for local runs: developers often rely on default acting as their dev configuration. You can also change the fallback with spring.profiles.default if you want a different name to serve as the no-profile baseline.

Interview note: Follow-up: "does activating prod still include default?" No — as soon as you set an active profile, default drops out entirely. Values you assumed would carry over from default will silently disappear.

Q6. How do profile-specific documents work inside a single YAML file?

You can split one YAML file into multiple documents with --- and gate each document with spring.config.activate.on-profile. Documents whose condition matches the active profile are applied; others are ignored. This keeps all environment config in one file when you prefer that layout.

The trap here is the syntax change: the old spring.profiles: key inside a multi-document YAML is deprecated. In Spring Boot 2.4+ and Spring Boot 3 you must use spring.config.activate.on-profile instead.

# application.yml — multi-document, profile-gated
spring:
  application:
    name: orders-service
---
spring:
  config:
    activate:
      on-profile: dev          # NOT the deprecated spring.profiles
datasource:
  url: jdbc:h2:mem:dev
---
spring:
  config:
    activate:
      on-profile: prod
datasource:
  url: jdbc:postgresql://db:5432/orders

Interview note: Trap: "your old config used spring.profiles: dev and stopped working after upgrading — why?" It was deprecated and replaced by spring.config.activate.on-profile. Naming the exact replacement key is what the interviewer is listening for.

Q7. What is a profile group and why is it useful?

A profile group, declared with spring.profiles.group, maps one profile name onto a set of profiles, so activating the group activates all of them. It lets you compose environments — activating prod can expand to prod, monitoring and audit in one flag.

Groups reduce activation to a single value in operations while keeping the underlying config modular. Instead of asking someone to remember --spring.profiles.active=prod,monitoring,audit, you define the group once and they activate prod.

# application.yml
spring:
  profiles:
    group:
      prod: prod,monitoring,audit   # activating prod pulls in all three

Interview note: Follow-up: "how is a group different from just listing profiles on the command line?" A group is defined once in configuration and reused, so the composition is versioned and consistent rather than depending on operators typing the same list correctly every time.

Q8. How do you set active profiles in tests?

Annotate the test class with @ActiveProfiles("test"). It activates that profile for the Spring test context only, so tests load application-test.yml and test-specific beans without affecting how the application runs in production. It composes with @SpringBootTest and slice tests like @DataJpaTest.

Keeping a dedicated test profile isolates test config — an in-memory database, disabled schedulers, a fake mail sender — from the profiles you use to run the app. It is the standard way to make integration tests deterministic.

@SpringBootTest
@ActiveProfiles("test")
class OrderServiceTest {
    // loads application-test.yml and @Profile("test") beans
}

Interview note: Trap: "how do you avoid profile sprawl as the app grows?" Keep a small, orthogonal set — environment profiles (dev/test/prod) plus a few feature profiles (monitoring, audit) — and compose them with groups rather than inventing a new profile per toggle. A dozen ad-hoc profiles is a smell.

How to prepare

Set up one small app with a base application.yml and two profile files, then run it three ways — no profile, --spring.profiles.active=dev, and the same via an environment variable — and watch which values win. Doing the override yourself makes the precedence rule from Q3 something you have observed rather than recited, and precedence is where profile answers usually fall apart. Then convert the two files into one multi-document YAML using spring.config.activate.on-profile so the deprecated-key trap in Q6 never catches you.

Build the configuration foundations on the Spring Boot learning path, and read this next to configuration properties and auto-configuration, since profiles, property binding and conditional configuration are usually explored together. A focused mock interview on externalized configuration is the fastest way to find whether your activation-and-precedence explanation holds up under pressure.

Frequently Asked Questions

What is a Spring Boot profile?
A profile is a named set of configuration and beans that Spring activates only in certain environments. It lets one build run differently across dev, test and prod by loading profile-specific property files and registering profile-conditional beans, so you keep environment differences out of code.
How do you activate a Spring Boot profile?
Set spring.profiles.active — via the SPRING_PROFILES_ACTIVE environment variable, a JVM system property (-Dspring.profiles.active=prod), a command-line argument (--spring.profiles.active=prod), or in a properties file. Command-line and environment values override file values, and you can activate several profiles as a comma-separated list.
What does @Profile do?
@Profile on a bean method or configuration class makes that bean register only when the named profile is active. It supports expressions like @Profile("prod") or negation @Profile("!prod"), so you can swap implementations — for example a real mail sender in prod and a no-op one in dev.
What is a profile group in Spring Boot?
A profile group, declared with spring.profiles.group, maps one profile name to a set of profiles so activating the group activates them all. For example spring.profiles.group.prod can expand to prod, monitoring and audit, so a single spring.profiles.active=prod turns on the whole set.
How do you set active profiles in a test?
Annotate the test class with @ActiveProfiles("test"), which activates that profile for the Spring test context. It keeps test configuration isolated from the profile used when the application runs normally, and it composes with test slices like @SpringBootTest and @DataJpaTest.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 16 July 2026 LinkedIn
Chat with us