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


 

[plain] view plain copy

  1. [2016-10-12 22:49:10,184] [DEBUG] (ScheduledAnnotationBeanPostProcessor.java:finishRegistration:207)   
  2. Could not find default TaskScheduler bean  
  3. org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined  
  4.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)  
  5.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)  
  6.     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:192)  
  7.     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:171)  
  8.     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:86)  
  9.     at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)  
  10.     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)  
  11.     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381)  
  12.     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335)  
  13.     at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:855)  
  14.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)  
  15.     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)  
  16.     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)  
  17.     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)  
  18.     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)  
  19.     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)  
  20.     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)  
  21.     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)  
  22.     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)  
  23.     at java.util.concurrent.FutureTask.run(FutureTask.java:266)  
  24.     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
  25.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
  26.     at java.lang.Thread.run(Thread.java:745)  
  27. [2016-10-12 22:49:10,188] [DEBUG] (ScheduledAnnotationBeanPostProcessor.java:finishRegistration:219)   
  28. Could not find default ScheduledExecutorService bean  
  29. org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined  
  30.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)  
  31.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)  
  32.     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:192)  
  33.     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:171)  
  34.     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:86)  
  35.     at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)  
  36.     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)  
  37.     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381)  
  38.     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335)  
  39.     at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:855)  
  40.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)  
  41.     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)  
  42.     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)  
  43.     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)  
  44.     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)  
  45.     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)  
  46.     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)  
  47.     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)  
  48.     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)  
  49.     at java.util.concurrent.FutureTask.run(FutureTask.java:266)  
  50.     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
  51.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
  52.     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的自己打印的一些信息罢了,不过没搞明白,为什么非要用异常的方式打出来,估计是为了看这清晰点吧。也或者,这里面有一些重要的信息需要提示开发者。具体是什么原因,只能有机会进一步再去了解了。

 

全部评论

相关推荐

想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务