Spring Profiles (@Profile, spring.profiles.active)
Fast recall
Quick revision
Core answer
prod profile is active. Profiles are activated via `spring.profiles.active` in application.properties, an environment variable (SPRING_PROFILES_ACTIVE), a JVM/CLI argument (--spring.profiles.active=prod), or programmatically. Multiple profiles can be active simultaneously. Spring Boot also loads profile-specific property files (application-{profile}.yml) automatically, and profile groups (Spring Boot 2.4+) let you activate a composite profile with one name.Practical ability to configure environment-specific beans (datasources, mocks, feature flags) and understanding of profile inheritance.
Hardcoding environment-specific values in beans instead of using profiles, or not knowing that multiple profiles can be active simultaneously.
Explain profile groups (spring.profiles.group.production=prod,monitoring) introduced in Spring Boot 2.4, and the difference between default profile and fallback profile behavior.
Interview-ready guide
Interview answer
Spring profiles let you register different bean graphs and load different property files based on the active environment — without changing code or rebuilding the artifact. The same JAR behaves differently in dev, staging, and production based on which profiles are active.
Profile-specific beans
@Profile("prod")on a@Beanor@Configurationclass — only registered whenprodis active@Profile("!prod")— registered in every profile except production; common for debug tooling and mock services- Multiple active profiles at once:
spring.profiles.active=prod,monitoring— beans from both profiles are registered
Activating profiles — priority order
- Command-line
--spring.profiles.active=prod— highest priority, overrides everything else - Environment variable
SPRING_PROFILES_ACTIVE=prod— Docker and Kubernetes standard spring.profiles.active=devinapplication.properties— developer default, lowest priority
Profile-specific property files
application-prod.propertiesloads automatically whenprodis active and overrides the baseapplication.propertiesfor any key present in both- Base file sets defaults; profile files override what is environment-specific
Profile groups (Spring Boot 2.4+)
spring.profiles.group.production=prod,monitoring,security— activateproductionand all three sub-profiles activate automatically
In tests, @ActiveProfiles("test") loads application-test.properties — typically with an H2 in-memory database, mock service URLs, and verbose logging. Tests never accidentally hit production resources.
Spring Profiles (@Profile, spring.profiles.active)
Direct Answer
Spring profiles let you register beans and load configuration conditionally based on the active environment (dev, staging, prod, test). A bean annotated with @Profile("prod") is only created when the prod profile is active. Profiles are activated via spring.profiles.active in application.properties, an environment variable (SPRING_PROFILES_ACTIVE), a JVM/CLI argument (--spring.profiles.active=prod), or programmatically. Multiple profiles can be active simultaneously. Spring Boot also loads profile-specific property files (application-{profile}.yml) automatically, and profile groups (Spring Boot 2.4+) let you activate a composite profile with one name.
Common Mistakes & Pitfalls
Hardcoding environment-specific values in beans instead of using profiles, or not knowing that multiple profiles can be active simultaneously.
Check your understanding
Answer these without looking back at the article.
- What is the 'default' profile in Spring and when is it active?
- How do you write a test that verifies prod-profile beans are correctly configured without running against a real database?
- What is the difference between @Profile and @ConditionalOnProperty for environment-specific beans?
- How does Spring resolve property precedence when both application-prod.properties and an env var set the same key?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.