在注释参数/参数中使用@ConfigurationProperties? (必须是编译时常量)

Use @ConfigurationProperties in Annotation parameter/ argument? (must be compile-time constant)

我正在尝试在注释中使用配置,如下所示:

@ConfigurationProperties("scheduling")
interface SchedulingConfiguration {
    val initialDelay: String
    val fixedDelay: String
}

@Singleton
class Worker(
    private val configuration: SchedulingConfiguration,
) {
    private val log = LoggerFactory.getLogger(javaClass)

    @Scheduled(initialDelay = configuration.initialDelay, fixedDelay = configuration.fixedDelay)
    fun fetchQueueEntry() {
        log.info("Fetching entry")
    }
}

我收到警告 An annotation argument must be a compile-time constant

有什么方法可以让它与 Micronaut 一起使用吗?

我通过浏览 Micronaut 文档并意外地发现了 属性 占位符,设法 运行 获得了它。这会很好用,即使感觉不到 'optimal'.

@Singleton
class Worker {
    private val log = LoggerFactory.getLogger(javaClass)

    @Scheduled(
        initialDelay = "${scheduling.initialDelay}",
        fixedDelay = "${scheduling.fixedDelay}"
    )    
    fun fetchQueueEntry() {
        log.info("Fetching entry")
    }
}

还可以定义默认值,如果配置文件或环境变量中不存在密钥,则将使用这些默认值:

    @Scheduled(
        initialDelay = "${scheduling.initialDelay:0s}",
        fixedDelay = "${scheduling.fixedDelay:10s}"
    )    

如果没有默认值并且没有使用 属性 占位符的配置,将在运行时抛出异常并且应用程序将关闭。