From 359315568a1fdcbb67539e63517c92d712e7dc76 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sun, 20 Apr 2014 08:30:39 -0700 Subject: [PATCH] Clarify handling and binding or YAML lists The docs related to YAML lists were out of date and lacked an example making it clear how to bind to them. See gh-501 --- .../main/asciidoc/spring-boot-features.adoc | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 71d782276b..ada30fb82f 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -372,25 +372,39 @@ Would be transformed into these properties: environments.prod.name=My Cool App ---- -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: +YAML lists are represented as property keys with `[index]` dereferencers, +for example this YAML: [source,yaml,indent=0] ---- - servers: - - dev.bar.com - - foo.bar.com + my: + servers: + - dev.bar.com + - foo.bar.com ---- Would be transformed into these properties: [source,properties,indent=0] ---- - servers=dev.bar.com,foo.bar.com - servers[0]=dev.bar.com - servers[1]=foo.bar.com + 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 + +[source,java,indent=0] +---- +@ConfigurationProperties(prefix="my") +public class Config { + private List servers = new ArrayList(); + public List getServers() { return this.servers; } +} +---- [[boot-features-external-config-exposing-yaml-to-spring]]