When I upgraded in SpringBoot, I have a problem:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.concurrent.Executor]: Illegal arguments to factory method 'getAsyncExecutor'; args: ; nested excep
My Code is :
@EnableAsync
@Configuration
public class ExecutorConfig implements AsyncConfigurer {
private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
@Primary
@Override
@Bean
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
// 设置核心线程数
threadPool.setCorePoolSize(10);
// 设置最大线程数
threadPool.setMaxPoolSize(100);
// 线程池所使用的缓冲队列
threadPool.setQueueCapacity(250);
// 等待任务在关机时完成--表明等待所有线程执行完
threadPool.setWaitForTasksToCompleteOnShutdown(true);
// 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止
threadPool.setAwaitTerminationSeconds(60);
// 线程名称前缀
threadPool.setThreadNamePrefix("MyAsync-");
// 初始化线程
threadPool.initialize();
return threadPool;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyAsyncExceptionHandler();
}
/**
* 自定义异常处理类
*
* @author hry
*
*/
class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
// 手动处理捕获的异常
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
System.out.println("-------------》》》捕获线程异常信息");
logger.info("Exception message - " + throwable.getMessage());
logger.info("Method name - " + method.getName());
for (Object param : obj) {
logger.info("Parameter value - " + param);
}
}
}
}
When I removed AsyncConfigurer, my SpringBoot project started normally. Why? My SpringBoot version was upgraded from 2.1.2 to 2.7.16 and SpringCloud version is 2.1.2.RELEASE upgraded to 2021.0.4.0. Thanks.
If you override
AsyncConfigurer
interface, it is not a fully managed Spring bean otherwise add the@Bean
annotation remove methodexecutor.initialize()
in the getAsyncExecutor() method if you want a fully managed bean.Refer to this document https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html