简体   繁体   中英

How to inject @Value properties into beans defined using Spring 5 and Kotlin Bean Definition DSL

I am developing a Spring Boot (2.0.0 M7) application using Kotlin, and need to define some beans taking advantage of the new DSL for bean definitions. I cannot come up with a way to inject values coming from @Value properties. Let's consider this simplified example:

fun beans() = beans {
    for (i in 1..10) {
        bean<String>("myString${i}" + someProperty) { "myString${i}" + someProperty}
    }
}

someProperty should come from something like this:

@Value("\${myProperty}") someProperty: String

How can I make it accessible to the beans {} DSL?

Solved accessing the values through the env variable:

fun beans() = beans {
    for (i in 1..10) {
        bean<String>("myString${i}" + env.getProperty("myProperty") { "myString${i}" + env.getProperty("myProperty")}
    }
}

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