【问题标题】:Kotlin compiler complains about using a SPeL expression in a property definition. WHY?Kotlin 编译器抱怨在属性定义中使用 SPeL 表达式。为什么?
【发布时间】:2017-11-14 08:41:34
【问题描述】:

当我尝试使用 SPeL 表达式注入一个值时,它在 Java 中有效,但在 Kotlin 中无效。编译器说

Error:(13, 25) Kotlin: An annotation parameter must be a compile-time constant

代码:

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

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

我的问题:

  1. 为什么 Kotlin 不喜欢 SPeL?这是一个构造注入(或者是它)并且不违反不变性。

  2. 这是编译器错误吗?消息无疑是错误的。

【问题讨论】:

    标签: spring spring-boot kotlin spring-el


    【解决方案1】:

    在 Kotlin 中的字符串中的${roots}string template,因此该字符串不是常量。

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

    @Value("#{new java.io.File('\${roots}')}")
    

    【讨论】:

    • 你成功了!谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-09-12
    • 2016-04-22
    • 2017-01-17
    • 2012-03-28
    • 2016-11-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    相关资源
    最近更新 更多