问题描述
在Spring的@Scheduled
cron
配置中,是否可以从propertyClass调用吸气剂(甚至是变量)?以下内容无法编译:
Is there a way to call a getter (or even a variable) from a propertyClass in Spring's @Scheduled
cron
configuration? The following doesn't compile:
@Scheduled(cron = propertyClass.getCronProperty())
或@Scheduled(cron = variable)
我想避免直接获取财产:
I would like to avoid grabbing the property directly:
@Scheduled(cron = "${cron.scheduling}")
推荐答案
简短的答案-不可能开箱即用.
Short answer - it's not possible out of the box.
在ScheduledAnnotationBeanPostProcessor
类中使用StringValueResolver
接口的实例处理在@Scheduled
批注中作为"cron表达式"传递的值.
The value passed as the "cron expression" in the @Scheduled
annotation is processed in ScheduledAnnotationBeanPostProcessor
class using an instance of the StringValueResolver
interface.
StringValueResolver
具有3种开箱即用的实现-对于Placeholder
(例如$ {}),对于Embedded
值和对于Static
字符串-都无法实现您想要的.
StringValueResolver
has 3 implementations out of the box - for Placeholder
(e.g. ${}), for Embedded
values and for Static
Strings - none of which can achieve what you're looking for.
如果必须不惜一切代价避免在批注中使用属性占位符,请摆脱批注并以编程方式构造所有内容.您可以使用ScheduledTaskRegistrar
来注册任务,而@Scheduled
注释实际上就是这样做的.
If you have to avoid at all costs using the properties placeholder in the annotation, get rid of the annotation and construct everything programmatically. You can register tasks using ScheduledTaskRegistrar
, which is what the @Scheduled
annotation actually does.
我建议使用可以通过并通过测试的最简单的解决方案.
I will suggest to use whatever is the simplest solution that works and passes the tests.
这篇关于Spring Boot @计划的cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!