Cron 表达式必须包含 6 个字段(在“#{systemEnvironment['db_cron']}”中找到 1 个)

Cron expression must consist of 6 fields (found 1 in "#{systemEnvironment['db_cron']}")

我正在尝试按如下方式设置 cron 计划注释:

@Scheduled(cron = "#{systemEnvironment['db_cron']}")
def void schedule() {

}

接下来设置环境变量为:

export db_cron="0 19 21 * * *"

但是,我收到运行时错误:

Cron expression must consist of 6 fields 
  (found 1 in "#{systemEnvironment['db_cron']}")

可能出了什么问题?


编辑

我也试过:@Scheduled(cron = "${db_cron}")

但是这个returns一个编译的时候报错:

/Users/snowch/repos/sales/Prospects/Snow/Plugwise/etl_service/src/main/groovy/com/ibm/etl/Application.groovy: 52: Expected '$db_cron' to be an inline constant of type java.lang.String in @org.springframework.scheduling.annotation.Scheduled
 @ line 52, column 23.
       @Scheduled(cron = "${db_cron}")
                         ^

/Users/snowch/repos/sales/Prospects/Snow/Plugwise/etl_service/src/main/groovy/com/ibm/etl/Application.groovy: -1: Attribute 'cron' should have type 'java.lang.String'; but found type 'java.lang.Object' in @org.springframework.scheduling.annotation.Scheduled
 @ line -1, column -1.
2 errors

:compileGroovy FAILED

FAILURE: Build failed with an exception.

我试过了@Scheduled(cron = "#{db_cron}")

但是运行时错误是:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: static org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.setSystemPropertiesMode() is applicable for argument types: (java.lang.Integer) values: [2]
Possible solutions: setSystemPropertiesMode(int), setSystemPropertiesModeName(java.lang.String)
    at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1373)
    at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1359)
    at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:50)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at com.ibm.etl.Application.main(Application.groovy:43)

注意环境变量设置如下:

snowch$ echo "$db_cron"
0 19 21 * * *

您应该像您一样设置环境变量:

export db_cron="0 19 21 * * *"

然后重新启动您的 ide 如果您正在使用或重新启动您的终端会话。

@Scheduled(cron = "${db_cron}")
def void schedule() {
   ...
}

我试过了,这是我的截图。一切都按预期进行...

对我来说,使用 JDK 11 和 Spring 2.3.3 添加双引号两个变量:

export db_cron="0 19 21 * * *"

和:

@Scheduled(cron = "${db_cron}")

注意: 也适用于 application.properties 个文件。