Spring Boot @计划的cron [英] Spring Boot @Scheduled cron

查看:160
本文介绍了Spring Boot @计划的cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆