Skip to content

Commit

Permalink
Merge branch '2.7.x'
Browse files Browse the repository at this point in the history
Closes gh-32197
  • Loading branch information
wilkinsona committed Aug 31, 2022
2 parents 09bd531 + 13edfba commit 4c01810
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Expand Up @@ -732,12 +732,10 @@ Default values can be specified using `@DefaultValue` on constructor parameters
The conversion service will be applied to coerce the annotation's `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`)

Expand Down
Expand Up @@ -21,10 +21,12 @@ import org.springframework.boot.context.properties.bind.DefaultValue
import java.net.InetAddress

@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<String>)
class Security(val username: String?, val password: String?,
@param:DefaultValue("USER") val roles: List<String>)

}
}
// end::code[]

0 comments on commit 4c01810

Please sign in to comment.