Why use profiles in Spring Boot?

Profiles in Spring Boot allow an application to load different configurations for different environments such as development, testing, staging, and production. This helps maintain a single codebase while using environment-specific settings and resources.

Key Points

Example

Consider a Spring Boot application with different database configurations:

Development Environment

Testing Environment

Production Environment

The same application code runs in all environments while only the configuration changes.

Configuration Example

application-dev.properties
application-test.properties
application-prod.properties

Activating a profile

spring.profiles.active=prod

Or using command line

java -jar app.jar --spring.profiles.active=prod

Using Profile-Specific Beans

@Profile("dev")
@Bean
public DataSource devDataSource() {
    return new H2DataSource();
}

@Profile("prod")
@Bean
public DataSource prodDataSource() {
    return new MysqlDataSource();
}

Benefits of Using Profiles

Real-World Example

An e-commerce application uses

Development

Production

The application behavior changes automatically based on the active profile.

Interview Tip

A concise interview answer is: Spring Profiles allow us to maintain different configurations for different environments such as development, testing, and production without modifying application code. This improves flexibility, simplifies deployment, and helps manage environment-specific resources efficiently.

Last updated: 11 November 2026

Previous question: Explain how Spring Boot profiles work.

Next question: What is aspect-oriented programming (AOP) in the Spring Framework?

More Spring Boot - Level II interview questions

All Spring Boot - Level II questions