find in path

Bind spring-boot configuration properties in Kotlin

2019-11-28spring-boot

This post showcases how to bind @ConfigurationProperties in spring-boot projects written in kotlin.

Spring Boot offers the @ConfigurationProperties annotation in order to allow binding and validating properties set externally (e.g. via application.yml file).

When writing a Spring Boot application in Kotlin programming language, the usage of data classes comes like a good candidate to be used for binding external properties that are to be used subseqently in the business logic code of the spring beans.

Nevertheless, using an approach like the following

data class Project(val name: String, val code: String)

will result at the start of the spring boot application in the following binding error:

The elements [project.code,project.name] were left unbound.

This happens because the binding is done by property setters (which can’t succeed when using val fields in kotlin).

There are several solutions to solve the binding of the external properties in kotlin classes.

The outcome of solutions is that the fields of the Project class will be kotlin friendly non-null fields and that the bootstrap of the spring application will fail in case that the required properties are not specified.

ConstructorBinding

By using the @ConstructorBinding annotation in the data class we can indicate that configuration properties should be bound using constructor arguments rather than by calling setters.

import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
data class Project(val name: String, val code: String)

Lateinit properties

Use lateinit modifier for allowing the properties of the class to be set at a later time.

class Project{
    lateinit var name: String
    lateinit var code: String


    override fun toString(): String {
        return "Project(name='$name', code='$code')"
    }
}