From afa2af1cf627b130efdf978879acc79478979469 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 14 Nov 2025 08:00:11 +0000 Subject: [PATCH] Correct docs on when setter is not needed with JavaBean binding The two main changes are: - A setter is no longer required when binding from a comma-separated list as long as the target list is mutable. The binder clears the list and then re-populates it. - A setter is now required for arrays, previously the values coming from configuration properties where merged into an existing array. Now, the array is replaced. Closes gh-43138 --- .../modules/reference/pages/features/external-config.adoc | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/external-config.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/external-config.adoc index 53c34160f68..236bd0de612 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/external-config.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/external-config.adoc @@ -767,12 +767,8 @@ NOTE: The properties that map to javadoc:org.springframework.boot.context.proper Such arrangement relies on a default empty constructor and getters and setters are usually mandatory, since binding is through standard Java Beans property descriptors, just like in Spring MVC. A setter may be omitted in the following cases: -* Maps, as long as they are initialized, need a getter but not necessarily a setter, since they can be mutated by the binder. -* Collections and arrays can be accessed either through an index (typically with YAML) or by using a single comma-separated value (properties). - In the latter case, a setter is mandatory. - We recommend to always add a setter for such types. - If you initialize a collection, make sure it is not immutable (as in the preceding example). -* If nested POJO properties are initialized (like the `Security` field in the preceding example), a setter is not required. +* Pre-initialized Maps and Collections, as long as they are initialized with a mutable implementation (like the `roles` field in the preceding example). +* Pre-initialized nested POJOs (like the `Security` field in the preceding example). If you want the binder to create the instance on the fly by using its default constructor, you need a setter. Some people use Project Lombok to add getters and setters automatically.