Skip to content

Commit 13edfba

Browse files
committed
Fix Kotlin example for empty @DefaultValue constructor binding
Closes gh-32177
1 parent 13c0cf7 commit 13edfba

File tree

2 files changed

+8
-7
lines changed
  • spring-boot-project/spring-boot-docs/src
    • docs/asciidoc/features
    • main/kotlin/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/constructorbinding/nonnull

2 files changed

+8
-7
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,10 @@ Default values can be specified using `@DefaultValue` on a constructor parameter
730730
The conversion service will be applied to coerce the `String` value to the target type of a missing property.
731731

732732
Referring to the previous example, if no properties are bound to `Security`, the `MyProperties` instance will contain a `null` value for `security`.
733-
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:
733+
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:
734734

735735
include::code:nonnull/MyProperties[tag=*]
736736

737-
738737
NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning.
739738
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`)
740739

spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/constructorbinding/nonnull/MyProperties.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import java.net.InetAddress
2323

2424
@ConstructorBinding
2525
@ConfigurationProperties("my.service")
26-
class MyProperties(val isEnabled: Boolean, val remoteAddress: InetAddress,
27-
@param:DefaultValue val security: Security) {
26+
// tag::code[]
27+
class MyProperties(val enabled: Boolean, val remoteAddress: InetAddress,
28+
@DefaultValue val security: Security) {
2829

29-
class Security(val username: String, val password: String,
30-
@param:DefaultValue("USER") val roles: List<String>)
30+
class Security(val username: String?, val password: String?,
31+
@param:DefaultValue("USER") val roles: List<String>)
3132

32-
}
33+
}
34+
// end::code[]

0 commit comments

Comments
 (0)