其他分享
首页 > 其他分享> > Spring Could not find unique TaskExecutor bean 错误

Spring Could not find unique TaskExecutor bean 错误

作者:互联网

这个错误其实是 debug 级别的错误,是不影响运行的。

错误栈为:

020-10-26 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 执行的方法。

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
@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 项目就可以告诉应用的上下文使用哪一个异步方法。

 

Spring-Could-01   https://www.ossez.com/t/spring-could-not-find-unique-taskexecutor-bean/614

 

标签:java,Spring,Could,springframework,bean,DefaultListableBeanFactory,taskExecutor,o
来源: https://www.cnblogs.com/huyuchengus/p/13887528.html