The @Profile annotation in Spring Boot marks a bean or configuration class so it is only registered when a specific named profile, such as dev, test, or prod, is active. It lets a single codebase carry environment-specific wiring without conditional logic scattered through the code.
A DataSource bean annotated with @Profile("dev") points to an in-memory H2 database, while a second bean annotated with @Profile("prod") points to the production PostgreSQL instance; Spring only creates the one matching the active profile.
@Configuration
public class DataSourceConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
return DataSourceBuilder.create()
.url("jdbc:postgresql://prod-host:5432/app")
.build();
}
}A concise interview answer is
@Profile lets me register beans conditionally based on the active Spring profile, so dev, test, and prod can each get their own configuration without if-else branching. I activate a profile with spring.profiles.active and Spring only creates the beans that match.
Last updated: 16 July 2026
Next question: Describe how you would set up integration tests for a Spring Boot application that interacts with an external API.