从属性文件设置 cron 计划而不是对其进行硬编码,并且仍然避免属性值必须是常量

问题描述 投票:0回答:1

我正在使用 SchedulerLock 在 Spring Boot 应用程序中创建计划。目前,该计划已硬编码为每 3 小时运行一次:

private final String everyThreeHours = "0 0 */3 * * *";

@Scheduled(cron = everyThreeHours)
public void runCleanupSchedule() throws IOException {
    ...
}

我想使

everyThreeHours
动态化,这样我就可以在每个环境的属性文件中拥有不同的计划。不幸的是,当我将其放入属性文件中时,我得到了
Attribute value must be constant
。有什么方法可以在我的属性中设置它而不是硬编码吗?

java spring spring-boot spring-scheduled
1个回答
0
投票

这应该有效

@Scheduled(cron = "${scheduled.everyThreeHours}")
public void runCleanupSchedule() throws IOException {
    ...
}

在您的 application.properties 添加 scheduled.everyThreeHours

scheduled.everyThreeHours=0 0 */3 * * *

您可以更改每个环境属性文件的值

© www.soinside.com 2019 - 2024. All rights reserved.