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

时间:2015-12-10 06:39:44

标签: spring cron properties-file spring-scheduled

我试图在我的春天@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}")

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

factoryBean.setCronExpression(“ 0 0/1 * 1/1 *?*”); 您必须在BeanFactory类中设置Cron Expresssion bcz,而setCronExpression是必需的