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
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/external-config.adoc
+8-1Lines changed: 8 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -821,7 +821,14 @@ include-code::MyProperties[]
821
821
In this setup, the presence of a single parameterized constructor implies that constructor binding should be used.
822
822
This means that the binder will find a constructor with the parameters that you wish to have bound.
823
823
If your class has multiple constructors, the javadoc:org.springframework.boot.context.properties.bind.ConstructorBinding[format=annotation] annotation can be used to specify which constructor to use for constructor binding.
824
-
To opt out of constructor binding for a class with a single parameterized constructor, the constructor must be annotated with javadoc:org.springframework.beans.factory.annotation.Autowired[format=annotation] or made `private`.
824
+
825
+
To opt-out of constructor binding for a class, the parameterized constructor must be annotated with javadoc:org.springframework.beans.factory.annotation.Autowired[format=annotation] or made `private`.
826
+
Kotlin developers can use an empty primary constructor to opt-out of constructor binding.
827
+
828
+
For example:
829
+
830
+
include-code::primaryconstructor/MyProperties[]
831
+
825
832
Constructor binding can be used with records.
826
833
Unless your record has multiple constructors, there is no need to use javadoc:org.springframework.boot.context.properties.bind.ConstructorBinding[format=annotation].
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinDefaultBindConstructorProviderTests.kt
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,18 @@ class KotlinDefaultBindConstructorProviderTests {
61
61
assertThat(bindConstructor).isNull()
62
62
}
63
63
64
+
@Test
65
+
fun`type with no param primary constructor and secondary params constructor should not use constructor binding`() {
66
+
val bindConstructor =this.constructorProvider.getBindConstructor(NoParamPrimaryWithParamsSecondaryProperties::class.java, false)
67
+
assertThat(bindConstructor).isNull()
68
+
}
69
+
70
+
@Test
71
+
fun`type with params primary constructor and no param secondary constructor should use constructor binding`() {
72
+
val bindConstructor =this.constructorProvider.getBindConstructor(ParamsPrimaryWithNoParamSecondaryProperties::class.java, false)
73
+
assertThat(bindConstructor).isNotNull()
74
+
}
75
+
64
76
@Test
65
77
fun`type with autowired secondary constructor should not use constructor binding`() {
66
78
val bindConstructor =this.constructorProvider.getBindConstructor(AutowiredSecondaryProperties::class.java, false)
@@ -127,6 +139,18 @@ class KotlinDefaultBindConstructorProviderTests {
0 commit comments