Kotlin编译器抱怨在属性定义中使用SPeL表达式.为什么?

Chr*_*rno 5 spring spring-el kotlin spring-boot

当我尝试使用SPeL表达式来注入一个值时,它可以在Java中运行,但不能在Kotlin中运行.编译说

错误:(13,25)Kotlin:注释参数必须是编译时常量

码:

@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)
}
Run Code Online (Sandbox Code Playgroud)

嗯...新闻快报Kotlin编译器:它是一个常数!编译器清楚地知道它是一个SPeL表达式并且不喜欢它.

我的问题:

  1. 为什么Kotlin不喜欢SPeL?这是施工注塑(或是它)并且不违反不变性.

  2. 这是编译器错误吗?这条消息是无可辩驳的错误.

zsm*_*b13 17

${roots}在Kotlin中的String中是一个字符串模板,因此String不是常量.

如果您希望String包含那些实际字符而不是解释为模板,则必须转义$:

@Value("#{new java.io.File('\${roots}')}")
Run Code Online (Sandbox Code Playgroud)