Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)
最近项目里面,用了spring的定时任务,一直以来,项目运行的不错。定时器也能正常使用。可是,今天启动项目测试的时候,盯着启动Log看了一阵子,突然间发现,启动的Log中居然有一个异常,虽然一闪而过,但是那熟悉的异常格式还是让我浑身一颤。这个项目一直运行的很好啊,没发现有什么问题,怎么会出现异常呢?
于是我仔细的翻啊翻啊,查啊找啊,嘿,还真的找到了两个异常:
- [2016-10-12 22:49:10,184] [DEBUG] (ScheduledAnnotationBeanPostProcessor.java:finishRegistration:207)
- Could not find default TaskScheduler bean
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
- at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:192)
- at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:171)
- at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:86)
- at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
- at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
- at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381)
- at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335)
- at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:855)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
- at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
- at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
- at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
- at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)
- at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
- at java.util.concurrent.FutureTask.run(FutureTask.java:266)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
- at java.lang.Thread.run(Thread.java:745)
- [2016-10-12 22:49:10,188] [DEBUG] (ScheduledAnnotationBeanPostProcessor.java:finishRegistration:219)
- Could not find default ScheduledExecutorService bean
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
- at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:192)
- at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:171)
- at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:86)
- at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
- at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
- at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381)
- at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335)
- at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:855)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
- at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
- at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
- at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
- at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)
- at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
- at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
- at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
- at java.util.concurrent.FutureTask.run(FutureTask.java:266)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
- at java.lang.Thread.run(Thread.java:745)
晕啊,很明显这是定时任务的异常啊。可是定时任务明明能够正常运行啊,怎么回事?
尝试着删掉了所有的定时任务配置,果然异常消失了。但是只要启用任何一个定时任务,异常都会再次出现。这可真可怕。作为一个强迫症患者,不能放任这样的异常存在,于是仔细的查找了半天,却依旧没有结果。最后只好放大招,读spring的源码啦,这一读,可好,差点没把我郁闷死。
为什么郁闷呢? 不知道读这篇文章的朋友们有没有细心的,有没有注意这两个异常有一个非常特别的地方。什么地方呢? 这两个异常的级别,不是ERROR 也不是 WARNING
居然是DEBUG
原来,Spring的定时任务调度器会尝试获取一个注册过的 task scheduler来做任务调度,它会尝试通过BeanFactory.getBean的方法来获取一个注册过的scheduler bean,获取的步骤如下:
1.尝试从配置中找到一个TaskScheduler Bean
2.寻找ScheduledExecutorService Bean
3.使用默认的scheduler
前两步,如果找不到的话,就会以debug的方式抛出异常,分别是:
logger.debug("Could not find default TaskScheduler bean", ex);
logger.debug("Could not find default ScheduledExecutorService bean", ex);
所以,日志中打印出来的两个异常,根本不是什么错误信息,也不会影响定时器的使用,只不过是spring的自己打印的一些信息罢了,不过没搞明白,为什么非要用异常的方式打出来,估计是为了看这清晰点吧。也或者,这里面有一些重要的信息需要提示开发者。具体是什么原因,只能有机会进一步再去了解了。