Spring Boot @Scheduled cron

lta*_*rne 11 cron spring schedule spring-boot

有没有办法在Spring的@Scheduled cron配置中从propertyClass调用getter(甚至变量)?以下内容无法编译:

@Scheduled(cron = propertyClass.getCronProperty()) 要么 @Scheduled(cron = variable)

我想避免直接抓住房产:

@Scheduled(cron = "${cron.scheduling}")
Run Code Online (Sandbox Code Playgroud)

hov*_*yan 9

简短的回答 - 它不可能开箱即用.

@Scheduled注释中作为"cron表达式"传递的值将ScheduledAnnotationBeanPostProcessor使用StringValueResolver接口实例在类中处理.

StringValueResolver有3个开箱即用的实现 - 对于Placeholder(例如$ {}),对于Embedded值和Static字符串 - 没有一个可以实现你正在寻找的东西.

如果必须在注释中使用属性占位符而不惜一切代价避免,请删除注释并以编程方式构造所有内容.您可以使用注册任务ScheduledTaskRegistrar,这是@Scheduled注释实际执行的操作.

我建议使用任何最简单的解决方案,并通过测试.