这个错误其实是 debug 级别的错误,是不影响运行的。
错误栈为:
15:27:57.726 DEBUG 12844 --- [nio-8080-exec-1] .s.a.AnnotationAsyncExecutionInterceptor : Could not find unique TaskExecutor bean org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.core.task.TaskExecutor' available: expected single matching bean but found 2: applicationTaskExecutor,taskScheduler at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:420) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:350) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343) at org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getDefaultExecutor(AsyncExecutionAspectSupport.java:233) at org.springframework.aop.interceptor.AsyncExecutionInterceptor.getDefaultExecutor(AsyncExecutionInterceptor.java:157) at org.springframework.aop.interceptor.AsyncExecutionAspectSupport.lambda$configure$2(AsyncExecutionAspectSupport.java:119)
原因和解决
简单来说就是在你的 @Configuration 中必须要实现一个 AsyncConfigurer 来为 @Async 指定一个 TaskExecutor 执行的方法。
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 org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
/**
* TODO
*
* @className: TaskExecutorConfig
**/
@Configuration
@Component
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {
/**
* Override Config
*
* @return
*/
@Override
public Executor getAsyncExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setMaxPoolSize(80);
taskExecutor.setQueueCapacity(100);
taskExecutor.initialize();
return taskExecutor;
}
}
可以将上面的方法拷贝放到你 Spring 的项目中,这样你的 Spring 项目就可以告诉应用的上下文使用哪一个异步方法。
原文链接:https://ossezhu.medium.com/spring-could-not-find-unique-taskexecutor-bean-错误-2fd6fa7693eb
标签:java,Spring,Could,springframework,bean,DefaultListableBeanFactory,org,import From: https://www.cnblogs.com/ya-cpp/p/17413238.html