which value precedes when using the new `spring.profiles.group.*` config?

Viewed 1968

I have 2 profiles ci and development which both set a prop db. I wonder which values "win" in the new Spring Boot 2.4 config handling when using a ci group like this:

spring.profiles.group.ci[0]: development 

---

spring:
  config.activate.on-profile: development
  db: a

---

spring:
  config.activate.on-profile: ci
  db: b

Question 1

If the environment sets the active profile to ci, which db value would be used? The value defined in ci or the one in development?

Question 2

Does it behave differently from question 1 if the environment sets active profiles explicitly to development, ci or ci, development?

Background

We are trying to migrate from profiles.include: ... to the new spring.profiles.group.* configuration. Formerly, we had the 'includestatement in theciprofile to include thedevelopment. However, by this itself the developmentvariables would override the ones inci- which to my perception is unexpected. Hence, we needed to specifyspring.active.profiles=development,cito let the values incioverride the ones indevelopment`. As far as I understood the changes in 2.4. are meant to address this unexpected behavior, but we still do not clearly see how things should behave and how the new config options should be used.

1 Answers

Spring boot documentation mentioned about Multi-Profile YAML documents :

You can specify multiple profile-specific YAML documents in a single file by using a `spring.profiles` key to indicate when the document applies. 

For example:

server:
    address: 192.168.1.100
---
spring:
    profiles: development
server:
    address: 127.0.0.1
---
spring:
    profiles: production
server:
    address: 192.168.1.120

The server.address property will be

  • 127.0.0.1 if the development profile is active.
  • 192.168.1.120 if the production profile is active.
  • If the development and production profiles are not enabled, then the value for the property will be 192.168.1.100

You can follow the same analogy to decide the winner.

More Information on changes bring in spring boot 2.4

Config file processing in Spring Boot 2.4

Group defining can be

spring.profiles.group.<groupname>=ci,development

To use the legacy way of working you can enable the property as :

spring.config.use-legacy-processing=true

For multi-document Propferties spring boot 2.4 comes with the syntax as (#---)

e.g.

test=value
#---
test=overriden-value

Migration Document Guidelines

If you use multi-document YAML files (files with ---- separators) then you need to be aware that property sources are now added in the order that documents are declared. With Spring Boot 2.3 and earlier, the order that the individual documents were added was based on profile activation order.

If you have properties that override each other, you need to make sure that the property you want to "win" is lower in the file. This means that you may need to reorder the documents inside your YAML.

Migration Guide

UPDATE I had simulated the multi profile properties Here is my answer based on the actual executions to your question highlighted

Question 1

If the environment sets the active profile to ci, which db value would be used? The value defined in ci or the one in development? Answer - db defined in ci

Question 2

Does it behave differently from question 1 if the environment sets active profiles explicitly to development, ci or ci, development? > It would not behave differently. If, you want explicitly to win a particular value define them as lower in document

Related