【发布时间】:2016-03-15 16:46:27
【问题描述】:
我试图在我的 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}")
有什么建议吗?
【问题讨论】:
-
你的
@PropertySource应该继续@Configuration类而不是@Service类。此外,您的代码似乎与您得到的错误不匹配,所以它是... -
@M.Deinum 这是一个服务类。那么我如何阅读这里的属性呢?已更新错误信息
-
使用属性 (
@Value) 与读取文件不同。你的@PropertySource现在几乎没用了,因为它什么也没做。此外,您还需要注册PropertySourcesPlaceholderConfigurer才能更换物品。 -
@M.Deinum 成功了。谢谢
标签: spring cron properties-file spring-scheduled