You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update `@ConfigurationProperties` constructor binding support to only
apply when a `@ConstructorBinding` annotation is present on either the
type or the specific constructor to use.
Prior to this commit we didn't have a good way to tell when constructor
binding should be used vs regular autowiring.
For convenience, an `@ImmutableConfigurationProperties` meta-annotation
has also been added which is composed of `@ConfigurationProperties` and
`@ConstructorBinding`.
Closesgh-18469
@@ -881,6 +881,7 @@ The example in the previous section can be rewritten in an immutable fashion as
881
881
882
882
private final List<String> roles;
883
883
884
+
@ConstructorBinding
884
885
public Security(String username, String password,
885
886
@DefaultValue("USER") List<String> roles) {
886
887
this.username = username;
@@ -899,11 +900,18 @@ The example in the previous section can be rewritten in an immutable fashion as
899
900
}
900
901
----
901
902
902
-
In this setup one, and only one constructor must be defined with the list of properties that you wish to bind and not other properties than the ones in the constructor are bound.
903
+
In this setup, the `@ImmutableConfigurationProperties` annotation is used to indicate that constructor binding should be used.
904
+
This means that the binder will expect to find a constructor with the parameters that you wish to have bound bind.
905
+
906
+
Nested classes that also require constructor binding (such as `Security` in the example above) should use the `@ConstructorBinding` annotation.
903
907
904
908
Default values can be specified using `@DefaultValue` and the same conversion service will be applied to coerce the `String` value to the target type of a missing property.
905
909
906
-
NOTE: To use constructor binding the class must not be annotated with `@Component` and must be enabled using `@EnableConfigurationProperties` or configuration property scanning instead.
910
+
TIP: You can also use `@ConstructorBinding` on the actual constructor that should be bound.
911
+
This is required if you have more than one constructor for your class.
912
+
913
+
NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning.
914
+
You cannot use constructor binding with beans that are created by the regular Spring mechanisms (e.g. `@Component` beans, beans created via `@Bean` methods or beans loaded using `@Import`)
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java
+7Lines changed: 7 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -29,11 +29,18 @@
29
29
* {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate
30
30
* some external Properties (e.g. from a .properties file).
31
31
* <p>
32
+
* Binding can is either performed by calling setters on the annotated class or, if
33
+
* {@link ConstructorBinding @ConstructorBinding} is in use, by binding to the constructor
34
+
* parameters.
35
+
* <p>
32
36
* Note that contrary to {@code @Value}, SpEL expressions are not evaluated since property
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrar.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindException.java
0 commit comments