springboot@scheduledcron
在Spring的springboot@scheduledcron,spring,cron,spring-boot,schedule,Spring,Cron,Spring Boot,Schedule,在Spring的@Scheduledcron配置中,是否有方法从propertyClass调用getter(甚至变量)?以下内容无法编译: @Scheduled(cron=propertyClass.getCronProperty())或@Scheduled(cron=variable) 我希望避免直接攫取财产: @Scheduled(cron = "${cron.scheduling}") 在/src/main/resources/application.properties中 my.cro
@Scheduled
cron
配置中,是否有方法从propertyClass调用getter(甚至变量)?以下内容无法编译:
@Scheduled(cron=propertyClass.getCronProperty())
或@Scheduled(cron=variable)
我希望避免直接攫取财产:
@Scheduled(cron = "${cron.scheduling}")
在/src/main/resources/application.properties中
my.cron.expression = 0 30 9 * * ?
简短的回答-这是不可能的开箱即用 在
@Scheduled
注释中作为“cron表达式”传递的值在ScheduledAnnotationBeanPostProcessor
类中使用StringValueResolver
接口的实例进行处理
StringValueResolver
有3种现成的实现—用于占位符
(例如${})、嵌入的
值和静态
字符串—所有这些都无法实现您想要的功能
如果您必须不惜一切代价避免在注释中使用properties占位符,请删除注释并以编程方式构造所有内容。您可以使用ScheduledTaskRegistrar
注册任务,而@Scheduled
注释实际上就是这样做的
我将建议使用最简单的解决方案,它可以工作并通过测试。创建一个获得该值的bean,然后将该bean自动连接到您的cron类中如何?propertyClass是一个bean,还是只包含静态方法的类?@bmoc该问题解决了另一个问题。这是我当前的代码。我的问题是关于使用变量或getter而不是
${My.cron.expression}
my.cron.expression = 0 30 9 * * ?