Skip to content

Commit b8babd4

Browse files
committed
Polish documentation on @ConfigurationProperties getters and setters
- Correct typo (coercable -> coercible) - Update description to reflect that Spring 4.1.5 supports the expansion of array properties and and a test that verifies the behaviour
1 parent d87bf70 commit b8babd4

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -529,19 +529,16 @@ the configuration of your application. For example:
529529
}
530530
----
531531

532-
NOTE: The getters and setters are advisable, since binding is via
533-
standard Java Beans property descriptors, just like in Spring
534-
MVC. They are mandatory for immutable types or those that are directly
535-
coercable from `String`. As long as they are initialized maps and
536-
collections need a getter but not necessarily a setter, for instance,
537-
since they can be mutated by the binder. Maps and collections can also
538-
be created (if there is a setter) and expanded (in any case), but
539-
arrays can not (so normally arrays need to be bound to a
540-
comma-separated value, but lists can be bound to individual
541-
elements). Nested POJO properties can also be created (so a setter is
542-
not mandatory) if they have a default constructor, or a constructor
543-
accepting a single value that can be coerced from String. Some people
544-
use Project Lombok to add getters and setters automatically.
532+
NOTE: The getters and setters are advisable, since binding is via standard Java Beans
533+
property descriptors, just like in Spring MVC. They are mandatory for immutable types
534+
or those that are directly coercible from `String`. As long as they are initialized,
535+
maps, collections, and arrays need a getter but not necessarily a setter since they
536+
can be mutated by the binder. If there is a setter, Maps, collections, and arrays can
537+
be created. Maps and collections can be expanded with only a getter, whereas arrays
538+
require a setter. Nested POJO properties can also be created (so a setter is not
539+
mandatory) if they have a default constructor, or a constructor accepting a single
540+
value that can be coerced from String. Some people use Project Lombok to add getters
541+
and setters automatically.
545542

546543
When the `@EnableConfigurationProperties` annotation is applied to your `@Configuration`,
547544
any beans annotated with `@ConfigurationProperties` will be automatically configured

spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ public void configurationPropertiesWithCharArray() throws Exception {
216216
equalTo("word".toCharArray()));
217217
}
218218

219+
@Test
220+
public void configurationPropertiesWithArrayExpansion() throws Exception {
221+
this.context = new AnnotationConfigApplicationContext();
222+
EnvironmentTestUtils.addEnvironment(this.context, "test.chars[4]:s");
223+
this.context.register(PropertyWithCharArrayExpansion.class);
224+
this.context.refresh();
225+
assertThat(this.context.getBean(PropertyWithCharArrayExpansion.class).getChars(),
226+
equalTo("words".toCharArray()));
227+
}
228+
219229
@Test
220230
public void notWritablePropertyException() throws Exception {
221231
this.context = new AnnotationConfigApplicationContext();
@@ -357,6 +367,23 @@ public void setChars(char[] chars) {
357367

358368
}
359369

370+
@Configuration
371+
@EnableConfigurationProperties
372+
@ConfigurationProperties(prefix = "test", ignoreUnknownFields = false)
373+
public static class PropertyWithCharArrayExpansion {
374+
375+
private char[] chars = new char[] { 'w', 'o', 'r', 'd' };
376+
377+
public char[] getChars() {
378+
return this.chars;
379+
}
380+
381+
public void setChars(char[] chars) {
382+
this.chars = chars;
383+
}
384+
385+
}
386+
360387
@Configuration
361388
@EnableConfigurationProperties
362389
@ConfigurationProperties(prefix = "test")

0 commit comments

Comments
 (0)