Reading property values to boolean in Kotlin/Spring with @Value

2.2k Views Asked by At

I've seen examples of how to do this in Java but I'm lacking an example in Kotlin. I want to read a property into a boolean using the @Value annotation from Spring

In my constructor I'm doing:

@Value("\${kafka.userComplexTopics:false}")
val useComplexTopicsString: String,

to pull out my String value and then in my class I have:

private val useComplexTopics = useComplexTopicsString.toBoolean()

I've been screwing around with SePL and can't make it work in one line.

1

There are 1 best solutions below

0
On

Spring should do this conversion for you. Unless you need the Stringified version, try setting your type to a Boolean.

@Component
class SomeClass(
    @Value("\${kafka.useComplexTopics:false}") val useComplexTopics: Boolean
) {
    init {
        println("UseComplexTopics: $useComplexTopics")
    }
}