spring application.yml reference list from another property

Solution

One way would be to use comma-separated lists in your profiles:

  • application-dev.yml
spring.profiles: dev
config.some.value: ELEMENT1,ELEMENT2
  • application-staging.yml
spring.profiles: staging
config.some.value: ELEMENT1,ELEMENT2,ELEMENT3

Then you should be able to access it in application.yml

some.value: ${config.some.value}

This solution doesn't require knowing list size upfront.

Explanation

The reason why this is working is described here. Specifically:

YAML lists are represented as comma-separated values (useful for simple String values) and also as property keys with [index] dereferencers, for example this YAML:
servers:
    - dev.bar.com
    - foo.bar.com
Would be transformed into these properties:
servers=dev.bar.com,foo.bar.com
servers[0]=dev.bar.com
servers[1]=foo.bar.com

In particular this means, that if you specify comma-separated list of strings in application.yml and define List<String> as value in @ConfigurationProperties, spring configuration properties binder will convert that comma-separated list of string to List<Strings>.