如何在Kotlin注解中使用Quarkus属性表达式?
在Kotlin注解中使用Quarkus属性表达式的正确方法
在Kotlin中,直接写"${storage.backend}"会触发字符串插值导致编译错误,转义\$又会让Quarkus无法识别表达式,正确的做法是使用Kotlin原生字符串字面量(三个双引号包裹):
@ApplicationScoped class SomeKotlinClass { @Inject @StorageBackend(implementation = """${storage.backend}""") private lateinit var storageBackend: StorageBackendInterface }
原理说明
Kotlin的原生字符串(""" """)不会对内部的$符号做插值解析,会将字符串原封不动地传递给注解。这样Quarkus就能接收到正确的${storage.backend}表达式,进而从application.properties中读取对应配置:
%int.storage-backend=redis %dev.storage-backend=in-memory
注:Quarkus支持配置键的横杠与驼峰互转,storage-backend和storage.backend是等价的。
内容的提问来源于stack exchange,提问作者Peter C. Glade
相关产品推荐
相关产品推荐

