当前位置: 首页 > 知识库问答 >
问题:

如何正确地将@ducedured()注释CRON表达式外化到我的application.properties文件中?它无法解析占位符

龚同
2023-03-14
@Scheduled(cron = "0 42 13 * * *")
public void runUpdateNotaryDistrictsJob() {
    LOGGER.info("SCHEDULED run of updateNotaryDistrictsJob STARTED");
    Map<String, JobParameter> confMap = new HashMap<>();
    confMap.put("time", new JobParameter(System.currentTimeMillis()));
    JobParameters jobParameters = new JobParameters(confMap);
    try {
        jobLauncher.run(updateNotaryDistrictsJob, jobParameters);
    }catch (Exception ex){
        LOGGER.error(ex.getMessage());
    }

}

然后,我试图将这个表达式外部化为一个占位符,它的值被定义到application.properties文件中。

因此,我在application.properties文件中定义了以下内容:

cron.expresion.runUpdateNotaryDistrictJob='0 32 11 * * *'

然后我以这样的方式更改了前面的@Schedured方法注释:

@Scheduled(cron = "${cron.expresion.runUpdateNotaryDistrictJob=}")
public void runUpdateNotaryDistrictsJob() {
    LOGGER.info("SCHEDULED run of updateNotaryDistrictsJob STARTED");
    Map<String, JobParameter> confMap = new HashMap<>();
    confMap.put("time", new JobParameter(System.currentTimeMillis()));
    JobParameters jobParameters = new JobParameters(confMap);
    try {
        jobLauncher.run(updateNotaryDistrictsJob, jobParameters);
    }catch (Exception ex){
        LOGGER.error(ex.getMessage());
    }

}

现在的问题是,在运行这个Spring Boot应用程序时,我获得了以下异常:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-10-26 11:30:43.556 ERROR 6117 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBatchExampleJobLauncher' defined in file [/home/andrea/git/notartel-api-batch/target/classes/com/notariato/updateInfo/SpringBatchExampleJobLauncher.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'runUpdateNotaryDistrictsJob': Could not resolve placeholder 'cron.expresion.runUpdateNotaryDistrictJob=' in value "${cron.expresion.runUpdateNotaryDistrictJob=}"
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:610) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.3.jar:2.5.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.3.jar:2.5.3]
    at com.notariato.updateInfo.UpdateInfoBatchApplication.main(UpdateInfoBatchApplication.java:42) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'runUpdateNotaryDistrictsJob': Could not resolve placeholder 'cron.expresion.runUpdateNotaryDistrictJob=' in value "${cron.expresion.runUpdateNotaryDistrictJob=}"
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:511) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:374) ~[spring-context-5.3.9.jar:5.3.9]
    at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:374) ~[spring-context-5.3.9.jar:5.3.9]
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:373) ~[spring-context-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:437) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.3.9.jar:5.3.9]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
    ... 15 common frames omitted

共有1个答案

柳钟展
2023-03-14
@Scheduled(cron = "${cron.expresion.runUpdateNotaryDistrictJob}")
cron.expresion.runUpdateNotaryDistrictJob = 0 32 11 * * *
 类似资料:
  • 我有一个Spring启动应用程序。当我在Spring工具套装4中单击“运行”按钮时,它可以正常工作。 但是当我以“maven install”运行时,我收到了错误消息: 我的应用属性的内容: 如果我将其更改为 它工作得很好。但我想从用户那里获取envVersionNum,它存储在.bash_profile(export envVersion Num=1.1.1)中。 pom.xml的内容: 家庭控

  • 我是Spring调度器的新手。我读了很多关于@ScheduledExecutorService和TimerTask的文章。 因此,据我所知,@ScheduledExecutorService和ScheduledExecutorService的功能大部分是相同的,但如果您的代码是在spring中,那么最好在代码中使用@ScheduledExecutorService。 所以我的问题是,假设我想在15

  • 问题内容: 我对春天还很陌生,所以请问这是一个愚蠢的问题。当我尝试启动程序时,出现以下错误:。执行以下代码时,将引发错误: 资源文件夹中存在一个名为的属性文件,其中包含主机和端口的信息。我不确定在哪里定义(如果有的话)。也许甚至没有定义,这就是问题所在。我需要将其更改为类似的东西还是缺少其他内容? 问题答案: 您没有正确读取属性文件。propertySource应该将参数传递为:或。将注释更改为:

  • 本文向大家介绍解释Java正则表达式中的子表达式“ [...]”,包括了解释Java正则表达式中的子表达式“ [...]”的使用技巧和注意事项,需要的朋友参考一下 子表达式“ [...] ”匹配括号中指定的任何单个字符。 例子1 输出结果 例子2 以下Java程序从用户那里接收5个字符串,并打印包含元音字母的字符串/单词。 输出结果

  • 问题内容: 我需要使用批注+正则表达式对电子邮件进行验证。我尝试使用以下内容: 但是,当电子邮件字段中的电子邮件地址不正确时,我不知道如何打印错误消息。有任何想法吗? 问题答案: 首先,您应该在注释中添加一个属性。假设您的邮件变量是某个类User的一部分: 然后,您应该定义一个验证器: 然后找到验证错误。

  • 这是我用来读取gradle项目中的application.properties文件的代码。 我的文件位于中 这就是我试图在IntelliJ中运行应用程序的方式