创建spring异步任务,报错No qualifying bean of type ‘org.springframework.core.task.TaskExecutor‘ available

2022-09-27 11:55:54

1、报错时的配置类和报错结果如下图:
在这里插入图片描述
在这里插入图片描述2、解决方式:在配置类中实现AsyncConfigurer接口,注册taskExecutor执行任务,即可,图和代码如下:
在这里插入图片描述

package com.rf.springboot01.concurrentAndThread.t3;import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.AsyncConfigurer;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;/**
 * @description:
 * @author: xiaozhi
 * @create: 2020-07-08 13:57
 */@Configuration@ComponentScan("com.rf.springboot01.concurrentAndThread.t3")@EnableAsyncpublicclassDemo7ConfigimplementsAsyncConfigurer{//注册执行器@Overridepublic ExecutorgetAsyncExecutor(){
        ThreadPoolTaskExecutor taskExecutor=newThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(80);
        taskExecutor.setQueueCapacity(100);
        taskExecutor.initialize();//如果不初始化,导致找到不到执行器return taskExecutor;}// 用于捕获异步异常@Overridepublic AsyncUncaughtExceptionHandlergetAsyncUncaughtExceptionHandler(){return null;}}

3、在重新启动,控制台输出无报错信息,如下图:
在这里插入图片描述

  • 作者:小志的博客
  • 原文链接:https://wwwxz.blog.csdn.net/article/details/107204948
    更新时间:2022-09-27 11:55:54