博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot使用@Scheduled启动报错:No qualifying bean
阅读量:6219 次
发布时间:2019-06-21

本文共 6482 字,大约阅读时间需要 21 分钟。

hot3.png

参考网页

解决方法一

解决方法二

报错信息

2018-06-12 14:02:24,709 [main] DEBUG [org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor] - 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:374)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:203)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:182)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:87)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)

at com.richfit.ruiche.RichfitCBMRest.main(RichfitCBMRest.java:39)

2018-06-12 14:02:24,711 [main] DEBUG [org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor] - Could not find default ScheduledExecutorService bean

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.util.concurrent.ScheduledExecutorService] is defined

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:374)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:224)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:182)

at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:87)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382)

at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)

at com.richfit.ruiche.RichfitCBMRest.main(RichfitCBMRest.java:39)

报错信息分析

报错信息有两个特点

1.报错是个debug信息;

2.工程项目最终还是正常启动了,而且项目中的自动任务运行正常,并未受到影响。

解决方法一--这个异常日志级别是 DEBUG,并不影响正常使用,可以在log4j日志级别上设置

在log4j 下的配置添加这行即可

log4j.logger.org.springframework.scheduling = INFO

效果--果然没有debug了,只是以info的形式打印了出来

2018-06-12 14:09:39,340 [main] INFO  [org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor] - No TaskScheduler/ScheduledExecutorService bean found for scheduled processing

解决方法二--既然报错提醒没有【TaskScheduler】,那么在SpringBoot的配置类中加上TaskScheduler即可

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.TaskScheduler;import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;@Configurationpublic class WebAppConfig extends WebMvcConfigurerAdapter{        	@Bean	public TaskScheduler scheduledExecutorService() {		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();		scheduler.setPoolSize(8);		scheduler.setThreadNamePrefix("scheduled-thread-");		return scheduler;	}}

★到底怎么回事--报错到底啥意思--可能Spring希望技术人员可以自己指定组件

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给自己打印的一些信息罢了。

★最终选择的解决方案

两个解决方法都可以,我最终选择的是【解决方法二】中的方法。

转载于:https://my.oschina.net/u/3866531/blog/1829117

你可能感兴趣的文章
监控磁盘读写状况
查看>>
Dockerfile 配置
查看>>
Python中创建虚拟环境
查看>>
foundation的sass版本
查看>>
PHP 代码生成图片验证码!你们值得拥有
查看>>
《编写可维护的JavaScript》第1部分编程风格小结
查看>>
如何成为一名业余程序员
查看>>
为Tomcat添加启动、停止、重启
查看>>
Nginx视频点播安装配置
查看>>
无状态地址自动配置
查看>>
各种排序算法的分析及java实现
查看>>
linux下退出VI的方法:不保存退出:q! 先保存后退出:wq
查看>>
小白之复习与提高3
查看>>
RAID技术
查看>>
centos6.5安装MySQL5.7(使用yum源安装方法)
查看>>
想要明白什么是key/value数据库
查看>>
模拟三次密码输入
查看>>
Linux系统下pid与pid文件及Hadoop更改pid文件存储位置
查看>>
POPTEST 150801 祝大家前途似锦
查看>>
htmlentities函数导致中文编码问题
查看>>