Springboot: Using yaml anchors across different spring profiles

Viewed 954

I have defined a set of default config properties and a YAML anchor &configDefault in my application.yml file.

config:
    test: &configDefault
        a: default_a
        b: default_b

I can use these default values in other spring.profiles by default but if I try to merge the anchor <<: *configDefault and then override value for config.test.b as:

spring:
    profiles: custom

config:
    <<: *configDefault
        b: custom_b

I get found undefined alias configDefault when starting the app. My expectation was that since all the default values port over to spring profiles I could re-use my predefined anchors too.

Not sure if I'm doing this wrong or if anchors are not intended to be used this way (I think this would be a nice feature to have to reduce duplication in configs.)

sample project on Github: https://github.com/rakeshsthakur/yaml-anchros, example properties file https://github.com/rakeshsthakur/yaml-anchros/blob/master/src/main/resources/application.yml

I have simplified projects properties on purpose. A more complex example case would have multiple anchors with defaults and overrides in the default spring profile itself. With additional overrides happening in environment-specific profiles when required.

Is this something I can do? Or should I just copy over the default properties with anchors to each profile separately

0 Answers
Related