Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined

2016年10月12日 23:15:15 oarsman 阅读数:15378 标签: spring task scheduler exception 更多

个人分类: JAVA开发

最近项目里面,用了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的自己打印的一些信息罢了,不过没搞明白,为什么非要用异常的方式打出来,估计是为了看这清晰点吧。也或者,这里面有一些重要的信息需要提示开发者。具体是什么原因,只能有机会进一步再去了解了。

【Spring 定时器】Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler]相关推荐

  1. spring boot 启动报错:No qualifying bean of type org.springframework.security.oauth2.provider.token.Toke

    spring boot启动报错: Exception encountered during context initialization - cancelling refresh attempt: o ...

  2. No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessageConverters‘

    Gateway中,使用feign调用接口,报No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMe ...

  3. Spring异常:NoSuchBeanDefinitionException: No qualifying bean of type ‘it.com.dao.AccountDao‘ available

    1.在测试Spring整合Mybatis,进行Junit测试时,遇到了以下报错 java.lang.IllegalStateException: Failed to load ApplicationC ...

  4. 【Spring常见错误】No qualifying bean of type

    报错信息: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam ...

  5. 【SpringCloud】feign.codec.EncodeException: No qualifying bean of type

    错误描述 在 Spring Cloud 项目中通过 Open Feign 远程调用时出现如下错误: feign.codec.EncodeException: No qualifying bean of ...

  6. Feign调用服务错误:No qualifying bean of type `HttpMessageConverters`

    Feign调用服务错误:No qualifying bean of type `HttpMessageConverters` 最近微服务项目整合Gateway和OpenFeign的时候出现报错 原因 ...

  7. Spring项目启动报错No qualifying bean of type [xxx] available: expected single matching bean but found 2

    启动报错: No qualifying bean of type [class1] available: expected single matching bean but found 2,class ...

  8. No qualifying bean of type ‘org.apache.rocketmq.spring.core.RocketMQTemplate‘ av

    前言 整合springboot+rocketMq报错 错误信息如下: org.springframework.beans.factory.NoSuchBeanDefinitionException: ...

  9. 在spring测试时候,出现BeanDefinitionException: No qualifying bean of type ‘com.xzq.mapper.GoodsDetailMapper‘

    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of ty ...

最新文章

  1. ASP.NET超凡的代码控制
  2. c++max函数怎么用_比函数还强大的Excel分列技巧,你知道怎么用吗
  3. matlab概率论实验 分别掷硬币1,基于Matlab的概率论仿真实验
  4. python下载在哪个盘_Windows下载安装python详情和注意事项
  5. CIKONSS-纯CSS实现的响应式Icon
  6. 为什么都开始流行将洗手台装在厕所外?
  7. 【数据分析】⚠️走进数据分析 3⚠️ Beautiful Soup 提取页面信息
  8. 【深度学习】深度学习:技术原理、迭代路径与局限
  9. python因子分析案例_python数据分析单因子分析
  10. oracle 查找不重复的数据,oracle不用distinct查找不重复记录和删除重复记录
  11. Apache 错误日记(Error Log)记录分析
  12. (Java)图解排序算法之归并排序
  13. 给网站添加代码统计-实现流量的变现
  14. 使body占有整个页面
  15. Java ee 网络层重点协议IP协议
  16. termux基础入门:论如何在手机上学习linux
  17. c语言ddv_max,STC单片机自制贴片元件拆焊台MAX6675控温程序
  18. DWDM光模块和CWDM光模块的区别
  19. 页面加载自动跳转功能 比如localhost8080
  20. 03 如何制作AE脚本的用户交互界面

热门文章

  1. 谭浩强C语言程序设计代码示例第5章(笔记)
  2. 楼宇节能改造?这套楼宇节能减排方案你得看看
  3. 数据结构入门篇:约夫瑟斯数据环问题
  4. 波士顿房价数据集.csv
  5. Uber上市第二日股价继续大跌 市值缩水至622亿美元
  6. hi3518移植librtmp
  7. 利用ai绘画小程序生成漫画脸的操作,附代码!,有没有可玩性----ai绘画小程序
  8. composition组合式API
  9. Notepad++设置个性化字体和视力保护色背景
  10. 常见电脑故障维修---主板篇