question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow @ConfigurationProperties binding for immutable POJOs

See original GitHub issue

Currently, 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:closed
  • Created 6 years ago
  • Reactions:139
  • Comments:58 (38 by maintainers)

github_iconTop GitHub Comments

24reactions
sdeleuzecommented, Nov 29, 2017

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 @ConfigurationProperties data classes with non-nullable properties.

Concretely, with the following configuration:

plugins {
  id "org.jetbrains.kotlin.plugin.noarg" version kotlinVersion
}
noArg {
    annotation("org.springframework.boot.context.properties.ConfigurationPro‌​perties")
}

It is possible to use the following mutable data classes:

@ConfigurationProperties("mixit")
data class MixitProperties(
    var baseUri: String,
    var contact: String,
    var drive: Drive,
    var aes: Aes) {

    data class Drive(
        var fr: DriveDocuments,
        var en: DriveDocuments)


    data class DriveDocuments(
        var sponsorform: String,
        var sponsor: String,
        var speaker: String,
        var press: String)

    data class Aes(
        var initvector: String,
        var key: String)
}

This issue is still needed to avoid the noArg trick and support immutable @ConfigurationProperties, but at least it provides a reasonably usable way to use @ConfigurationProperties with 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 BeanBinder that supports immutable classes (including support for data classes) using Kotlin reflection. I will share the code later when I have something usable.

22reactions
sdeleuzecommented, Mar 8, 2019

Thanks a lot Boot team for fixing that!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found