spring-bootspring-boot-configuration

Referencing configuration section from other place in spring-boot application.yaml


I'm configuring spring boot kafka streams in application.yaml. I need to configure properties of the output topics:

producer:
  topic.properties:
    cleanup.policy: compact
    retention.ms: 604800000

Because I have the same configuration across the whole file I want to make single point where to define values:

my:
 policy: compact
 retention: 604800000
producer:
  topic.properties:
    cleanup.policy: ${my.policy}
    retention.ms: ${my.retention}

But the topic.properties is just generic map passed to underlying kafka library. To make the configuration more flexible I would like to reference the my section from the producer.topic.properties. So when new kafka property is added then only my section is updated.

I tried:

producer:
  topic.properties: ${my}

But this doesn't work - ${my} is replaced by my.toString() and configuration fails on getting String where Map is expected.

I'm looking for some section placeholder. For example in OpenAPI Spec you can do something similar to:

my:
 policy: compact
 retention: 604800000
producer:
  topic.properties:
     $ref: '/my'

I know basic YAML doesn't support references. But is there something in spring-boot allowing to reference other config sections?


Solution

  • Spring Boot supports YAML anchors, therefore it's possible to do the following:

    .my: &my 
      policy: compact
      retention: 604800000
    producer:
      topic.properties: *my