来自属性文件的 Spring @Scheduled cron 详细信息 - 异常 [英] Spring @Scheduled cron details from property file - Exception

查看:21
本文介绍了来自属性文件的 Spring @Scheduled cron 详细信息 - 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 spring @Scheduled 方法中定义 cron 细节

I was trying to define the cron details in my spring @Scheduled method

@Service
@PropertySource("classpath:application.properties")
public class CacheRefreshService {

@Scheduled(cron = "${api.refresh.cron}")
     public void refreshJob() throws Exception {
        LOGGER.info("Started Refresh");
        //do something
     }
}

在我的 application.properties 中

And in my application.properties

#Refresh
api.refresh.cron =0 29 11 * * ?

当我与 @Scheduled 一起定义 cron 详细信息时,它运行良好.但是当我这样做时,它无法从属性文件中读取值并抛出以下错误.

When I define the cron details along with @Scheduled, it is running fine. But when I do this, it is not able to read the value from the properties file and the below error is thrown.

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'refreshJob': Cron expression must consist of 6 fields (found 1 in "${api.refresh.cron}")

请问有什么建议吗?

推荐答案

将以下内容添加到我的 ApplicationContext 解决了问题..

Adding the below to my ApplicationContext resolved the issue..

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
       return new PropertySourcesPlaceholderConfigurer();
    }

这篇关于来自属性文件的 Spring @Scheduled cron 详细信息 - 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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