SpringBoot使用@Scheduled启动报错:No qualifying bean

参考网页

解决方法一

https://blog.csdn.net/beflyabot/article/details/51497588

解决方法二

https://blog.csdn.net/zhengsaisai/article/details/80136003

报错信息

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;

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{    
    
	@Bean
	public TaskScheduler scheduledExecutorService() {
		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
		scheduler.setPoolSize(8);
		scheduler.setThreadNamePrefix("scheduled-thread-");
		return scheduler;
	}

}

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

https://blog.csdn.net/oarsman/article/details/52801877

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值