java spring scheduled_java – 如何在Spring中使用@Scheduled(cron)和SpEL?

我有一个方法,我想要春天安排 – 为此我使用@Scheduled注释 – 更确切地说,我使用的是cron表达式.

我的cron表达式位于名为scheduler.properties的属性文件中.

当我用它作为占位符@Scheduled(cron =“${cron}”)时 – 一切都很好;但我想使用SpEL(@Scheduled(cron =“#{scheduler [‘cron’]}”)),它不起作用 – 抛出以下异常:java.lang.IllegalArgumentException:cron表达式必须由6组成字段(在{{scheduler [‘cron’]}中找到1)

我在这做错了什么?

编辑:

这是我的属性文件中的cron表达式:cron = 0 0/1 * * *?

这是我得到的堆栈跟踪:

java.lang.IllegalArgumentException:cron表达式必须包含6个字段(在#{scheduler [‘cron’]}中找到1)

在org.springframework.scheduling.support.CronSequenceGenerator.parse(CronSequenceGenerator.java:233)

在org.springframework.scheduling.support.CronSequenceGenerator.< init>(CronSequenceGenerator.java:81)

在org.springframework.scheduling.support.CronTrigger.< init>(CronTrigger.java:54)

在org.springframework.scheduling.support.CronTrigger.< init>(CronTrigger.java:44)

在org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:188)

在org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:209)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:1)

在org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)

在org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)

在org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929)

在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)

at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)

在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)

在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)

在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)

at org.apache.catalina.core.StandardContext $1.call(StandardContext.java:5226)

在org.apache.catalina.core.StandardContext $1.call(StandardContext.java:5221)

at java.util.concurrent.FutureTask $Sync.innerRun(FutureTask.java:334)

at java.util.concurrent.FutureTask.run(FutureTask.java:166)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

at java.util.concurrent.ThreadPoolExecutor $Worker.run(ThreadPoolExecutor.java:603)

在java.lang.Thread.run(Thread.java:722)

第二次编辑:

似乎spring试图解析下面的字符串,因为cron experssion“#{scheduler [‘cron’]}”对实际的cron表达式本身进行了解释.

解决方法:

根据错误消息,属性文件中的cron表达式的值不正确.

它不符合预期的语法.

该值应包含六个字段,看起来像这样.

* 10 * * * *

这是抛出此异常的代码

/**

* Parse the given pattern expression.

*/

private void parse(String expression) throws IllegalArgumentException {

String[] fields = StringUtils.tokenizeToStringArray(expression, " ");

if (fields.length != 6) {

throw new IllegalArgumentException(String.format(""

+ "cron expression must consist of 6 fields (found %d in %s)", fields.length, expression));

}

可能无法在Annotation中使用spEL外部化cron配置.

替代方案是使用XML或使用cron表达式.

标签:java,spring,annotations,cron

来源: https://codeday.me/bug/20190621/1249988.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值