Explore Topics

Profiles & External Configuration (YAML/Properties)

Spring Boot allows developers to easily manage different environments (e.g., development, testing, production) by using profiles. This feature enables externalized configuration, making it easier to adjust settings for different stages of the application lifecycle without modifying the code.

Profiles in Spring Boot allow you to group beans and configurations based on the environment in which the application is running. For example, you may have different database credentials for development and production. Spring Boot allows you to activate these profiles through the application.properties or application.yml files.

Define Profiles

You can define profiles in your configuration files in two common formats: properties or YAML.

Using application.properties

You can define a profile by specifying properties for each environment in different files. For example:

  • application.properties (common/default configurations):

  • application-dev.properties (development profile):

  • application-prod.properties (production profile):

To activate a specific profile, you can set the spring.profiles.active property:

Using application.yml

YAML files provide a cleaner way to organize your configurations.

  • application.yml (common/default configurations):

  • application-dev.yml (development profile):

  • application-prod.yml (production profile):

Use Profiles in Code

You can conditionally enable beans or configuration classes based on active profiles. Use the @Profile annotation to bind beans to specific profiles:

Similarly, in your application class, you can check which profile is active: