diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc index d536ed72111c..db4ba1a2aac2 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc @@ -730,11 +730,10 @@ Default values can be specified using `@DefaultValue` on a constructor parameter The conversion service will be applied to coerce the `String` value to the target type of a missing property. Referring to the previous example, if no properties are bound to `Security`, the `MyProperties` instance will contain a `null` value for `security`. -If you wish you return a non-null instance of `Security` even when no properties are bound to it, you can use an empty `@DefaultValue` annotation to do so: +To make it contain a non-null instance of `Security` even when no properties are bound to it (when using Kotlin, this will require the `username` and `password` parameters of `Security` to be declared as nullable as they do not have default values), use an empty `@DefaultValue` annotation: include::code:nonnull/MyProperties[tag=*] - NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning. You cannot use constructor binding with beans that are created by the regular Spring mechanisms (for example `@Component` beans, beans created by using `@Bean` methods or beans loaded by using `@Import`) diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/constructorbinding/nonnull/MyProperties.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/constructorbinding/nonnull/MyProperties.kt index 806cc3125ec6..302c24835d9e 100644 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/constructorbinding/nonnull/MyProperties.kt +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/constructorbinding/nonnull/MyProperties.kt @@ -23,10 +23,12 @@ import java.net.InetAddress @ConstructorBinding @ConfigurationProperties("my.service") -class MyProperties(val isEnabled: Boolean, val remoteAddress: InetAddress, - @param:DefaultValue val security: Security) { +// tag::code[] +class MyProperties(val enabled: Boolean, val remoteAddress: InetAddress, + @DefaultValue val security: Security) { - class Security(val username: String, val password: String, - @param:DefaultValue("USER") val roles: List) + class Security(val username: String?, val password: String?, + @param:DefaultValue("USER") val roles: List) -} \ No newline at end of file +} +// end::code[]