Question
How to properly initialize ConfigurationProperties in Spring Boot with Kotlin?
Currently I do like in the example below:
@ConfigurationProperties("app")
class Config {
var foo: String? = null
}
But it looks pretty ugly and actually foo
is not a var
iable, foo is
constant val
ue and should be initialized during startup and will not
change in the future.
Answer
With new Spring Boot 2.2 you can do like so:
@ConstructorBinding
@ConfigurationProperties(prefix = "swagger")
data class SwaggerProp(
val title: String, val description: String, val version: String
)
And don't forget to include this in your dependencies in build.gradle.kts
:
dependencies {
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}