从属性文件中弹出@Scheduled cron细节 - 异常

问题描述 投票:2回答:2

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

@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中

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

当我使用@Scheduled定义cron细节时,它运行正常。但是当我这样做时,它无法从属性文件中读取值,并抛出以下错误。

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

有什么建议吗?

spring cron properties-file spring-scheduled
2个回答
2
投票

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

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

1
投票
factoryBean.setCronExpression("0 0/1 * 1/1 * ? *"); 

你必须在BeanFactory类中设置Cron Expresssion bcz,setCronExpression是强制性的

© www.soinside.com 2019 - 2024. All rights reserved.