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. oracle10安装网络需求警告,安装oracle10出现的问题,求解!!!!!(在线等)
  2. 《Apache Kafka》学习笔记
  3. 1085 Perfect Sequence
  4. FCGF-基于稀疏全卷积网络的点云特征描述子提取(ICCV2019)
  5. NeurIPS 2019 获奖论文出炉,微软华人学者Lin Xiao 获经典论文奖
  6. java金额小写转大写(8.2)
  7. 2.oracle物理结构,oracle实验2oracle物理结构管理
  8. 【数据库系统】SQL程序设计的层次
  9. 腾讯2012实习生招聘面试题:矩阵中A移动到B一共有多少走法
  10. [OfficeExcel] OfficeExcel2010 第18讲 Lookup--浅谈数组
  11. Jupyter Notebook设置密码且遇到提示:ImportError: DLL load failed while importing _ssl
  12. 酷比魔方iwork1x全套驱动
  13. 打开文件安全警告怎么关闭?
  14. 利用spring的jdbcTemplate处理blob、clob
  15. 经典的大学学习生活心得
  16. 中断处理过程示意图_中断和中断处理流程
  17. 硬核黑科技告诉你,漫威 10 年到底有多少个角色?
  18. alpine 服务器系统,alpine系统装zerotier-one
  19. 小甲鱼python小游戏“飞机大战”源码素材
  20. 交换机芯片笔记1.2

热门文章

  1. 【ASE+python学习】-批量识别石墨烯团簇结构中的吡啶氮,并删除与其相连的氢
  2. Nginx 对同一应用做分流后Session超时问题解决
  3. #Loj121 动态图连通性
  4. 地图坐标转换(火星坐标系、百度坐标系、谷歌、腾讯、高德等坐标)
  5. 程序员专用壁纸汇总:电脑+手机
  6. WordZ:Word终结者,基于Google API的文档自动化 电子合同发票流水账单线上集成方案
  7. vb程序设计题(新)
  8. pg PostGIS教程:空间关系
  9. 杨辉三角java实验目的,杨辉三角java
  10. 通过品牌和型号查询车型信息接口文档