javaspringspring-boot

Migrating to spring.config.activate.on-profile in Spring Boot 3


I am in the process of migrating an old Spring Boot application from 2.3.5.RELEASE to version 3.1.2 and attempting to update a module that uses configuration file format to align with the new profile activation mechanism introduced in Spring Boot 2.4. Previously, I was using spring.profiles to activate different configurations based on the active profile. However, in the new version, it seems that spring.config.activate.on-profile is the recommended approach.

Here is an excerpt from my original configuration file (src/test/resources/example/auto-loaded/config-file.yaml):

auto-loaded:
  yaml-config-file:
    yaml-config-file-property: default value

---
spring:
  profiles: some-env

auto-loaded:
  yaml-config-file:
    yaml-config-file-property: value for some-env

---
spring:
  profiles: some-other-env

auto-loaded:
  yaml-config-file:
    yaml-config-file-property: value for some-other-env
    some-other-env-property: expected not to be loaded

I attempted to migrate this to the new format as follows:

auto-loaded:
  yaml-config-file:
    yaml-config-file-property: default value

---
spring:
  config:
    activate:
      on-profile: some-env

auto-loaded:
  yaml-config-file:
    yaml-config-file-property: value for some-env

---
spring:
  config:
    activate:
      on-profile: some-other-env

auto-loaded:
  yaml-config-file:
    yaml-config-file-property: value for some-other-env
    some-other-env-property: expected not to be loaded

After updating the configuration file, I am not seeing the expected behavior when activating the profiles some-env and some-other-env. The properties don't seem to be loaded or overridden as they were before the migration.

Here are my questions:

Is my updated configuration file format correct according to the new profile activation mechanism in Spring Boot 3? Are there additional steps needed to ensure that the profile-specific configurations are loaded and applied correctly? more details can be found here https://github.com/negadras/config-properties (test class DefaultConfigLoaderTest and DefaultConfigLoaderWithProfileTest)


Solution

  • In this case you should use

    spring:
      profiles:
        active: some-env
    

    More details about it you can find in documentation

    About

    spring:
      config:
        activate:
          on-profile: some-env
    

    Also all that you need is described in documentation (can be used to mark a configuration file segment profile-specific.)