What is the Spring Boot @Profile annotation used for?

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.

Key Points

Example

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();
    }
}

Interview Tip

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

Previous question: You are tasked with designing a series of new RESTful endpoints for a complex product inventory system. What best practices would you follow to ensure scalability, maintainability, and performance?

Next question: Describe how you would set up integration tests for a Spring Boot application that interacts with an external API.

More Spring Boot - Level IV (Advanced) interview questions

All Spring Boot - Level IV (Advanced) questions