我正在使用 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
。有什么方法可以在我的属性中设置它而不是硬编码吗?
这应该有效
@Scheduled(cron = "${scheduled.everyThreeHours}")
public void runCleanupSchedule() throws IOException {
...
}
在您的 application.properties 添加 scheduled.everyThreeHours
scheduled.everyThreeHours=0 0 */3 * * *
您可以更改每个环境属性文件的值