Spring Profiles

Spring Profiles provide a powerful way to segregate parts of your application configuration and beans based on the environment (e.g., dev, test, prod).


What are Spring Profiles?

Spring Profiles allow you to define different configurations for different environments and conditionally load beans or properties.

java
@Profile("dev")
@Bean
public DataSource devDataSource() {
    // Dev-specific bean
}


What Do Profiles Control?


Why do we need Spring Profiles?

  1. Environment-specific logic without code duplication.
  2. Avoid hard-coding config values (e.g., DB credentials).
  3. Maintain clean code separation for dev/staging/prod.
  4. Enable feature toggling.

When to use Spring Profiles?