Allow @ConfigurationProperties binding for immutable POJOs
See original GitHub issueCurrently, it seems we are forced to use Kotlin classes with mutable nullable properties and default constructor with @ConfigurationProperties while idiomatic Kotlin code would be using classes with immutable properties initialized via constructor. I think there is a way to supporting that by leveraging kotlin-reflect library like jackson-module-kotlin do.
More concretely, in MiXiT app I would like to be able to convert this MixitProperties class implementation to:
@ConfigurationProperties("mixit")
class MixitProperties(
val baseUri: String,
val admin: Credential,
val drive: Drive
)
class Credential(
val username: String,
val password: String
)
class Drive(
val fr: DriveDocuments,
val en: DriveDocuments
)
class DriveDocuments(
val sponsorform: String,
val sponsor: String,
val speaker: String,
val press: String
)
We could imagine to support optional properties by using nullable types, like val sponsorform: String? for example.
I will be happy to help. I have also already worked with @apatrida who maintains Jackson Kotlin module, he may provide us some guidance I think.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:139
- Comments:58 (38 by maintainers)
Top Results From Across the Web
Guide to @ConfigurationProperties in Spring Boot - Baeldung
A quick and practical guide to @ConfigurationProperties ... in the main Spring application class to bind the properties into the POJO:
Read more >Immutable @ConfigurationProperties - java - Stack Overflow
The documentation shows an example. You just need to declare a constructor with the fields to bind (instead of the setter way) and...
Read more >24. Externalized Configuration - Spring
Spring Boot uses some relaxed rules for binding Environment properties to @ConfigurationProperties beans, so there does not need to be an exact match...
Read more >Using @ConfigurationProperties in Spring Boot - amitph
The @ConfigurationPropertis annotation is used on a class or a @Bean method to map external properties configurations into the class or bean. The...
Read more >Binding external configurations to POJO classes - CalliCoder
Creating the Application · Defining Properties · Binding external properties to a POJO class using @ConfigurationProperties · Enabling ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Update: the solution proposed bellow does not work with nested classes so in practice is pretty useless 😦
Good news, while working on this issue, I found that the no-arg plugin allows to use Kotlin mutable
@ConfigurationPropertiesdata classes with non-nullable properties.Concretely, with the following configuration:
It is possible to use the following mutable data classes:
This issue is still needed to avoid the
noArgtrick and support immutable@ConfigurationProperties, but at least it provides a reasonably usable way to use@ConfigurationPropertieswith Kotlin + Spring Boot 2.0 regardless if we fix this issue in time for 2.0 or not.In parallel, I am working on a Kotlin
BeanBinderthat supports immutable classes (including support for data classes) using Kotlin reflection. I will share the code later when I have something usable.Thanks a lot Boot team for fixing that!