For would like to have certain YAML documents activate only if two profiles are enabled. At the same time I may define a YAML documents for each Profiles. The desired effect is that YAML document activated by the conditional & of the two profiles should have a higher precedence then single profile YAML documents.

Ex.) If spring.profiles.active=ProfileA,ProfileB, I would like the value of spring.message to be "ProfileA an ProfileB activated!!!"

spring:
  profiles:
    active:
      - ProfileA
      - ProfileB

management:
  endpoints:
    web:
      exposure:
        include: '*'
---

spring:
  profiles: ProfileA & ProfileB

  message: ProfileA and ProfileB activated!!!

---

spring:
  profiles: ProfileA

  message: ProfileA activated!!!

---

spring:
  profiles: ProfileB

  message: ProfileB activated!!!

I would expect the positive profiles in an expression would contribute to the precedence but not anything negative.

For example spring.profiles: ProfileA & ProfileB & !ProfileC I would like this YAML document to have higher precedence then ProfileA and ProfileB. The precedence of ProfileC does not matter because it is activated.

I have attached an example project: profile-expressions.zip

Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-multi-profile-yaml

Comment From: wilkinsona

Thanks for the suggestion but, unfortunately, I don’t think we can implement this. It would break the documented behaviour:

The YAML documents are merged in the order in which they are encountered. Later values override earlier values.

This area of the codebase is one of the most complex and problematic and this documented behaviour has some known issues (see #18199, for example). We will hopefully be able resolve #18199 in due course.

Even if it what’s proposed here was not a breaking change, I think it would be foolhardy to add to the existing complexity to order the documents based on the specificity of their profile expressions. It would be difficult to implement and I think it would be difficult for users to understand. For example, if profile b is active, does a & b or c & b win?

Thanks anyway for the suggestion.

Comment From: ddcruver

The order in which they occur part confused me because it did not matter where in the YAML file I put the spring.profiles: ProfileA & ProfileB YAML document. It always had lower priority.

Regardless thanks for you time.

I revisited it and a better workaround and maybe a more explicit way would be to setup the profiles like this instead:


spring:
  profiles:
    active:
      - ProfileA
      - ProfileB

management:
  endpoints:
    web:
      exposure:
        include: '*'
---

spring:
  profiles: ProfileA & ProfileB

  message: ProfileA and ProfileB activated!!!

---

spring:
  profiles: ProfileA & !ProfileB

  message: ProfileA activated!!!

---

spring:
  profiles: ProfileB & !ProfileA

  message: ProfileB activated!!!

---

Comment From: ddcruver

As an update it looks like this was implemented (with a different property) in Spring Boot 2.4 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide#profile-specific-documents. Thanks all.