You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 01:18:14