Skip to content

Commit 4c01810

Browse files
committed
Merge branch '2.7.x'
Closes gh-32197
2 parents 09bd531 + 13edfba commit 4c01810

File tree

2 files changed

+8
-8
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
-8
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,10 @@ Default values can be specified using `@DefaultValue` on constructor parameters
732732
The conversion service will be applied to coerce the annotation's `String` value to the target type of a missing property.
733733

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

738737
include::code:nonnull/MyProperties[tag=*]
739738

740-
741739
NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning.
742740
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`)
743741

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
@@ -21,10 +21,12 @@ import org.springframework.boot.context.properties.bind.DefaultValue
2121
import java.net.InetAddress
2222

2323
@ConfigurationProperties("my.service")
24-
class MyProperties(val isEnabled: Boolean, val remoteAddress: InetAddress,
25-
@param:DefaultValue val security: Security) {
24+
// tag::code[]
25+
class MyProperties(val enabled: Boolean, val remoteAddress: InetAddress,
26+
@DefaultValue val security: Security) {
2627

27-
class Security(val username: String, val password: String,
28-
@param:DefaultValue("USER") val roles: List<String>)
28+
class Security(val username: String?, val password: String?,
29+
@param:DefaultValue("USER") val roles: List<String>)
2930

30-
}
31+
}
32+
// end::code[]

0 commit comments

Comments
 (0)