Skip to content

Latest commit

 

History

History
133 lines (100 loc) · 5.99 KB

09_ProfilesConfiguration.adoc

File metadata and controls

133 lines (100 loc) · 5.99 KB

Spring Boot - Profiles and Configuration

Configure and customize Spring Boot applications.

Activating Profiles

Spring Boot allows activation of profiles. When no specific profile is set to be active, Spring Boot selects what is known as default profile.

Profiles in Spring Boot can be activated in several ways:

Command Line

Add a -Dspring.profile.active=<PROFILE_NAME> when running the app

Environment Variable

Set and export an environment variable
spring_profiles_active = <PROFILE_NAME>

System Property

Set a SystemProperty
System.setProperty("spring.profiles.active", "<PROFILE_NAME>")

ConfigurableEnvironment

Invoke a setActiveProfiles(<PROFILE_NAME>) on the @Autowired instance of the ConfigurableEnvironment


Spring External Configuration

Spring provides multiple ways to configure portions of the application. As of Spring Boot v2.4.2, there are 14 different ways to configure a Spring Boot application, with a clear hierarchy of how each configuration overrides values set in previous configuration formats.

A lot more is covered at the Spring Boot Reference - External Configuration.

In the Todo examples only the YAML variants are covered.

  1. Default application.yaml
    application.yaml

    1. The default yaml configuration file contains a portion separated by a ---. These are profile sections.

    2. The spring: config: activate: on-profile determined when the section is activated.

    3. A value for todo.title is set when the profile is activated.

  2. Application yaml for development environment
    application-development.yaml

    1. Loaded when the active profiles includes development.

    2. A value for todo.descriptiveText is set when the development profile is activated.

  3. Application yaml for production environment
    application-production.yaml

    1. Loaded when the active profiles includes production.

    2. A value for todo.descriptiveText is set when the production profile is activated.

  4. Loading the custom configurations into the context
    ProfileConfigurationProperties.java

    1. The @ConfigurationProperties sets the prefix for the properties handled by this class.

    2. The title and descriptiveText are loaded into the instance, which is also injected into the context, thanks to the @Component annotation.

The content in application.yaml is always loaded. The Spring active profile determines if the suffixed files also get loaded.

For instance, setting the active profile to production, will cause the application-production.yaml to be loaded.


Spring Profiles

Spring Profiles set on beans and configurations allow for selective loading. This is set with a @Profile annotation on a bean or configuration. When a Spring Profile is activated, the bean or configuration that is marked by the profile is activated and added to the context.

While the actual Swagger documentation context is not yet covered, as a preview, let us examine the usage of profiles in the Swagger configuration.

  1. Default Swagger configuration
    TodoDefaultServiceSwaggerConfig.java

    1. This configuration is loaded when no profile is specifically made active.

    2. The @Profile annotation at the class specifies that this is activated for the default profile.

  2. Development Swagger configuration
    TodoDevelopmentServiceSwaggerConfig.java

    1. This configuration is loaded when the development profile is made active (and the default profile is inactive).

    2. The @Profile annotation at the class specifies that this is activated for the development profile and not when the default active.

    3. This configuration uses the @Autowired ProfileConfigurationProperties instance.

  3. Production Swagger configuration
    TodoProductionServiceSwaggerConfig.java

    1. This configuration is loaded when the production profile is made active (and the default profile is inactive).

    2. The @Profile annotation at the class specifies that this is activated for the production profile and not when the default active.

    3. This configuration uses the @Autowired ProfileConfigurationProperties instance.


Exercise Lab

Lab

  1. Update the application.yaml to include an activation for development and production.

    1. Set a lab:swaggerTitle: for development and for production.

  2. Create yaml files for environments:

    1. development and production.

    2. set the value lab:description to match the environment.

  3. Create empty Swagger configurations with the right profiles.


Prev TOC Next

Working with the Controller Layer

TOC

Adding RESTful Documentation