使用@Scheduled和@EnableScheduling,但给出NoSuchBeanDefinitionException
我按照非常简单的在线示例在春季设置了cron作业,但我每次都会在我的Tomcat启动日志中收到此错误:
2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:191 -
Could not find default TaskScheduler bean org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:202 - Could not
find default ScheduledExecutorService bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying
bean of type [org.springframework.scheduling.TaskScheduler] is defined
以及用于实现 cron 的 2 个 java 类:
-
@Configuration类:
@Configuration @EnableScheduling public class ClearTokenStoreCronEnable { final static Logger log = LoggerFactory.getLogger(ClearTokenStoreCronEnable.class); private @Autowired TokenStoreRepository tokenStoreRepository; }
-
和 Cron 工作类:
@Service public class ClearTokenStoreWorkerService { final static Logger log = LoggerFactory.getLogger(ClearTokenStoreWorkerService.class); private @Autowired TokenStoreRepository tokenStoreRepository; //@Scheduled(fixedDelay=5000) //run daily at midnight @Scheduled(cron = "0 0 * * * *") public void tokenStoreTable() { log.debug("tokenstore table truncated - start"); tokenStoreRepository.deleteAll(); log.debug("tokenstore table truncated - end"); } }
顺便说一句,cron 作业在午夜运行,但似乎在其他时间也随机运行。不确定这是一个错误还是我的cron表达式是错误的:@Scheduled(cron = "0 0 * * * *")
我现在主要担心的是,为什么我会收到 ScheduledAnnotationBeanPostProcessor
错误?它正在寻找一个 TaskScheduler 和 ScheduledExectorService。我只需要每天发射一次。我没有进行任何并发处理或需要多个线程的地方。最终,这些错误是有害的还是我需要修复它们?