弹簧与kotlin预定注释从属性文件获取参数

时间:2018-03-08 10:15:33

标签: java spring kotlin

我正在使用带有kotlin的spring 5,我有以下代码

@Scheduled(cron = "${job.notification.expiring.x}")
fun notify() {

}

并在application.yml

job:
  notification:
    expiring.x: 0 0 12 1/1 * ?

@schedulled的行上,cron参数注释Intellij说

An annotation parameter must be a compile-time constant.

如何解决这个问题,在Java中,属性是在运行时加载的。

1 个答案:

答案 0 :(得分:5)

你需要转义Kotlin中的$字符,因为它用于字符串模板:

@Scheduled(cron = "\${job.notification.expiring.x}")

请参阅本页底部的字符串模板部分:

https://kotlinlang.org/docs/reference/basic-types.html