简体   繁体   中英

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

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.

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")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM