-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Description
I'm trying to wrap my head around how to use configuration properties functionality of Spring Boot while trying to keep things concise, robust and safe.
What I'd like to have is a configuration properties definition that is:
- immutable (final fields and class)
- concise (records)
- robust (validated)
- safe (primarily null safe by using both validation and Optional).
Something like
@Validated
@ConfigurationProperties("fooBar")
@ConstructorBinding
record FooBarProperties(@NotNull Foo foo,
Optional<Bar> bar) {
}
Due to limitations of records of keeping exact types for accessors and parameters user is forced to use Optional on record parameter definitions. My understanding is this clashes with
The use of java.util.Optional with @ConfigurationProperties is not recommended as it is primarily intended for use as a return type. As such, it is not well-suited to configuration property injection. For consistency with properties of other types, if you do declare an Optional property and it has no value, null rather than an empty Optional will be bound.
Can support for this use case be added?