The Kotlin compiler complains about the use of an SPeL expression in a property definition. WHAT FOR?
When I try to use an SPeL expression to input a value, it works with Java but not Kotlin. The compiler says
Error: (13, 25) Kotlin: annotation parameter must be compile-time constant
code:
@SpringBootApplication
open class DeDup(@Value("#{new java.io.File('${roots}')}") val roots: Set<File>,
@Value("algo") val hashAlgo: String,
@Value("types")val fileTypes: List<String>) {
}
fun main(args: Array<String>) {
SpringApplication.run(DeDup::class.java, *args)
}
Mmm ... news flash Kotlin compiler: it's permanent! The compiler clearly knows this SPeL expression and doesn't like it.
My questions:
-
Why doesn't Kotlin like SPeL? This is construct injection (or is it) and does not violate immutability.
-
Is this a compiler error? The message is irrefutably wrong.
source to share
${roots}
inside String in Kotlin is a string template , so String is not constant.
If you want the string to contain these actual characters and not be interpreted as a pattern, you will need to avoid $
:
@Value("#{new java.io.File('\${roots}')}")
source to share