Spring TaskExecutor bean 错误
这个错误其实是 debug 级别的错误 , 是不影响运行的 。
错误栈为:
020-10-26 15:27:57.726 DEBUG 12844 --- [nio-8080-exec-1] .s.a.AnnotationAsyncExecutionInterceptor : Could not find unique TaskExecutor beanorg.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 执行的方法 。
package com.insight.sco.service.config;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;/** * AsyncConfigurer for Spring * * @author YuCheng Hu */@Configuration@Component@EnableAsyncpublic class TaskExecutorConfig implements AsyncConfigurer{/*** Override Config** @return*/@Overridepublic Executor getAsyncExecutor(){ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();taskExecutor.setCorePoolSize(10);taskExecutor.setMaxPoolSize(80);taskExecutor.setQueueCapacity(100);taskExecutor.initialize();return taskExecutor;}}
【Spring TaskExecutor bean 错误】可以将上面的方法拷贝放到你 Spring 的项目中 , 这样你的 Spring 项目就可以告诉应用的上下文使用哪一个异步方法 。
文章插图
- Spring security CSRF 跨域访问限制问题
- Spring Boot搭建的一个在线文件预览系统
- 面试官:问你一个,Spring事务是如何传播的?
- 对Spring MVC接口进行Mock测试
- Spring Cloud Alibaba之 Sentinel
- SpringBoot+MyBatis+MySQL读写分离实现
- SpringBoot构造流程源码分析:Web应用类型推断
- 搭建私有Sentry日志收集系统并集成到springboot
- Spring事务原理?事务在方法间如何传播?为什么会失效?
- SpringBoot扫描不到组件?给你提供几种方案