1

I have to integrate a list in a YAML config file in Spring Boot, and don't see how to proceed.

I already saw other questions related : Spring Boot yaml configuration for a list of strings

And have the same issue.

I applied the solution and worked around, and found the solution a little tricky.

Is there a way to make lists work with the @Value ?

And if not now, is it expected in future ?

Thanks a lot.

Community
  • 1
  • 1
Tetragramato
  • 73
  • 2
  • 8
  • Would properties file format be simpler than YAML ? – Jay Oct 19 '15 at 10:11
  • It seems this has not been fixed in Spring and there are no plans to do: https://github.com/spring-projects/spring-framework/pull/747 – Marged Oct 19 '15 at 10:22
  • Too bad, because the way to make it works is very tricky. Maybe i'm going to make my list as String with comma, even if i think this is not a good way :/ – Tetragramato Oct 19 '15 at 11:13

3 Answers3

3

According to this documentation you can do a list in yaml. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml

YAML lists are represented as property keys with [index] dereferencers, for example this YAML:

my:
   servers:
       - dev.bar.com
       - foo.bar.com

Would be transformed into these properties:

my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

To bind to properties like that using the Spring DataBinder utilities (which is what @ConfigurationProperties does) you need to have a property in the target bean of type java.util.List (or Set) and you either need to provide a setter, or initialize it with a mutable value, e.g. this will bind to the properties above

@ConfigurationProperties(prefix="my")
public class Config {

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

    public List<String> getServers() {
        return this.servers;
    }
}
mark
  • 154
  • 9
0
https://www.youtube.com/watch?v=d6Scea1JdMg&t=9s

Please refer to the above link. Maybe it helps, shown how to read different data types in application.yml in the Spring Boot.

Vanka Manikanth
  • 69
  • 1
  • 10
0

There is a related GitHub thread: @Value annotation should be able to inject List from YAML properties. The issue has been closed, and according to a comment in a duplicate issue, they're not considering implementing the support now. It will be reopened once they decide to work on it.

Until then, you can go the way described in @mark's answer, using @ConfigurationProperties, also mentioned on GitHub.

Prectron
  • 282
  • 2
  • 11