What is a Spring profile and how do you configure environment-specific beans?
Learn what a Spring profile is and how to configure environment-specific beans with @Profile and application-{profile} files for clean dev, test and prod setup.
Expected Interview Answer
A Spring profile is a named logical grouping of beans and configuration that Spring activates only in specific environments, letting one codebase behave differently in dev, test, and production.
You mark beans or configuration classes with @Profile("dev") so they are registered only when that profile is active, and you supply per-environment settings in files like application-dev.properties or application-prod.yml. Profiles are activated via spring.profiles.active, an environment variable, a JVM flag, or programmatically, and multiple profiles can be active at once. This keeps secrets, datasource URLs, and mock-versus-real beans cleanly separated instead of scattered behind if-statements.
- One artifact runs unchanged across environments
- Cleanly swaps real and mock/stub beans
- Keeps environment secrets and URLs separated
- Avoids conditional if/else configuration code
- Supports composing multiple active profiles
- Makes local dev and CI reproducible
AI Mentor Explanation
A Spring profile is like a captain choosing a playing eleven for the conditions: on a dry turning pitch he picks extra spinners, on a green seaming track he loads pace. Same squad, different lineup selected by the match situation, just as the active profile selects which beans get fielded.
Step-by-Step Explanation
Step 1
Annotate environment beans
Mark beans or @Configuration classes with @Profile("dev") / @Profile("prod") so they register only under that profile.
Step 2
Create per-profile property files
Add application-dev.yml, application-test.yml, and application-prod.yml holding environment-specific values like datasource URLs.
Step 3
Set the active profile
Choose the profile via spring.profiles.active in properties, an env var SPRING_PROFILES_ACTIVE, or the JVM flag -Dspring.profiles.active.
Step 4
Provide sensible defaults
Keep shared values in application.yml and let profile files override only what differs, optionally with a default profile.
Step 5
Verify at startup
Check the startup log line reporting the active profiles and confirm the expected beans were created.
What Interviewer Expects
- Clear definition of a profile as conditional bean registration
- Knowledge of @Profile and application-{profile} files
- Multiple ways to activate a profile
- Awareness that multiple profiles can be active together
- A real dev-versus-prod bean-swapping example
Common Mistakes
- Confusing profiles with @ConfigurationProperties binding
- Hardcoding environment logic with if-statements instead of @Profile
- Forgetting the application-{profile} filename convention
- Assuming only one profile can be active at a time
- Committing production secrets into profile files in source control
Best Answer (HR Friendly)
“A Spring profile lets one application behave differently depending on where it runs, such as development versus production. You label certain settings and components for each environment, then tell Spring which environment is active, and it automatically loads the right ones.”
Code Example
@Configuration
public class DataSourceConfig {
@Bean
@Profile("dev")
public DataSource h2DataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
@Bean
@Profile("prod")
public DataSource postgresDataSource(
@Value("${db.url}") String url) {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(url);
return ds;
}
}
// application.yml
// spring:
// profiles:
// active: devFollow-up Questions
- How do you activate more than one profile at the same time?
- What is the difference between @Profile and @ConfigurationProperties?
- How does Spring Boot pick up application-{profile}.yml files?
- How would you set the active profile in a Docker or Kubernetes deployment?
- What is a 'default' profile and when does it apply?
MCQ Practice
1. Which annotation registers a bean only when a given profile is active?
@Profile tells Spring to create the bean only when the named profile is among the active profiles.
2. Which property activates one or more profiles in Spring Boot?
spring.profiles.active accepts a comma-separated list of profiles to activate at startup.
3. Which file provides overrides only when the 'prod' profile is active?
Spring Boot loads application-{profile}.yml, so application-prod.yml applies only under the prod profile.
Flash Cards
What is a Spring profile? — A named group of beans and config that Spring activates only in specific environments.
Which annotation gates a bean by profile? — @Profile("name") on the bean or @Configuration class.
How do you set the active profile? — spring.profiles.active, the SPRING_PROFILES_ACTIVE env var, or -Dspring.profiles.active.
Naming convention for profile property files? — application-{profile}.yml or application-{profile}.properties.
Can multiple profiles be active at once? — Yes; supply a comma-separated list and their beans and properties combine.
Continue Learning
Related Interview Questions
What is dependency injection in Spring and how does the IoC container work?
medium
How does Spring Boot handle application configuration with application.properties and profiles?
medium
What is Spring AOP and how do aspects, advice, and pointcuts work?
medium
What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
easy