我试图在我的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}")
有什么建议吗?
将以下内容添加到我的ApplicationContext解决了问题..
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
factoryBean.setCronExpression("0 0/1 * 1/1 * ? *");
你必须在BeanFactory
类中设置Cron Expresssion bcz,setCronExpression
是强制性的