在Spring的@Scheduled cron配置中,有没有办法从propertyClass调用getter (甚至是变量)?以下代码无法编译:
@Scheduled(cron = propertyClass.getCronProperty())或@Scheduled(cron = variable)
我想避免直接获取属性:
@Scheduled(cron = "${cron.scheduling}")发布于 2016-04-05 23:58:34
简而言之--开箱即用是不可能的。
在@Scheduled注释中作为"cron expression“传递的值在ScheduledAnnotationBeanPostProcessor类中使用StringValueResolver接口的实例进行处理。
StringValueResolver有3个开箱即用的实现--针对Placeholder (例如${})、针对Embedded值和针对Static字符串--没有一个能够实现您想要的结果。
如果您必须不惜一切代价避免在注释中使用属性占位符,请删除注释并以编程方式构造所有内容。您可以使用ScheduledTaskRegistrar注册任务,这正是@Scheduled注释实际做的事情。
我会建议使用任何最简单的解决方案,只要能起作用并通过测试。
发布于 2021-07-24 07:33:05
如果您不想从属性文件中检索cron表达式,您可以按如下所示进行编程:
// Constructor
public YourClass(){
Properties props = System.getProperties();
props.put("cron.scheduling", "0 30 9 * * ?");
}这允许你使用你的代码,而忽略任何更改:
@Scheduled(cron = "${cron.scheduling}")发布于 2016-04-05 15:52:10
@Component
public class MyReminder {
@Autowired
private SomeService someService;
@Scheduled(cron = "${my.cron.expression}")
public void excecute(){
someService.someMethod();
}
}在/src/main/resources/application.properties中
my.cron.expression = 0 30 9 * * ?https://stackoverflow.com/questions/36204858
复制相似问题