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")
@EnableAsync
public class Demo7Config implements AsyncConfigurer {
//注册执行器
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setMaxPoolSize(80);
taskExecutor.setQueueCapacity(100);
taskExecutor.initialize();//如果不初始化,导致找到不到执行器
return taskExecutor;
}
// 用于捕获异步异常
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
3、在重新启动,控制台输出无报错信息,如下图: