2017-06-12 41 views
2

當我嘗試使用SPeL表達式注入一個值時,它可以在Java中使用,但不能在Kotlin中使用。編譯器說Kotlin編譯器抱怨在屬性定義中使用SPeL表達式。爲什麼?

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

嗯...快訊科特林編譯器:它是一個常數!編譯器清楚地知道它是一個SPeL表達式,並且不喜歡它。

我的問題:

  1. 爲什麼不喜歡科特林SPEL?這是一種施工注入(或是否),並且不違反不變性。

  2. 這是一個編譯器錯誤?這個信息是無可辯駁的錯誤。

回答

9

${roots}在科特林字符串內部是string template,因此該字符串不是常數。

如果你希望字符串包含這些實際的字符,而不是被解釋爲模板,你必須逃離$

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

你釘它!謝謝 –

相關問題