spring application.yml reference list from another property
Asked Answered
C

4

17

I have property file application-dev.yml with content:

spring.profiles: dev
config.some.value:
- ELEMENT1
- ELEMENT2

and another application-staging.yml with content:

spring.profiles: staging
config.some.value:
- ELEMENT1
- ELEMENT2
- ELEMENT3

so I basically do not know size of list. When I reference this list in main application.yml like this:

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

I get Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'value'. How to reference it correctly?

Chinchilla answered 25/1, 2019 at 13:50 Comment(1)
so what do you wanna do exactly? you wanna include staging and dev in default application.yml?Wiley
F
26

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>.

Fishtail answered 4/2, 2019 at 22:16 Comment(0)
C
0
MyProfile:
SomeValues: 
    - ELEMENT1
    - ELEMENT2
     -ELEMENT3
    - ELEMENT4

---
MyProfile:   
someValues: 
    - ELEMENT1
    - ELEMENT2



@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class YAMLConfig {


    private List<String> SomeValues= new ArrayList<>();

    // standard getters and setters

}

To Access properties

  @Autowired
   private YAMLConfig myConfig;


 private List<String> SomeValues= myConfig.SomeValues();

This worked for me

Cordate answered 31/1, 2019 at 12:22 Comment(1)
I cannot change code. I need to do it only in .yml files.Chinchilla
C
0

When using String arrays the first and only element CAN be joined by commas - the final result is the same as a list. Meaning that you can set your variable like this:

config.some.value: ELEMENT1, ELEMENT2, ELEMENT3

then, at your profile section, you can reference the configuration value as it were a normal string:

someValues: ${config.some.value}

Constitutional answered 1/2, 2019 at 1:15 Comment(0)
L
0

This is the best way to create .yml file with multiple values:

spring:
  profiles: dev
  config:
    some:
      values: ELEMENT1,ELEMENT2

When we are using .yml files, we recommend pulling apart each word. To read values use:

@Value("${spring.config.some.values}")    
private String[] values;

I hope it helps you.

Lituus answered 5/2, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.