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

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

问题描述

我试图在我的@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 * * ?

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

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天全站免登陆