gpt4 book ai didi

java - 从 4.2.0.RC3 升级到 4.2.0.RELEASE 时出现 Spring Async 问题

转载 作者:IT老高 更新时间:2023-10-28 13:51:52 25 4
gpt4 key购买 nike

我有一个使用 spring(4.2.x) 工件 spring-webmvc、spring-messaging、spring-websocket 的 Web 应用程序

我的 spring 配置 java 类中有以下 @Enable* 注释

@EnableWebMvc
@EnableWebSocketMessageBroker
@EnableAsync
@EnableMBeanExport

WebSocket 用于向浏览器客户端广播消息。而且很少有用@Async注解的异步方法

该应用程序在 Spring 版本 4.2.0.RC3 上运行良好。但是当我将它更改为 GA 版本 4.2.0.RELEASE 时,我在启动时得到以下异常。如果我删除 @EnableAsync 它可以正常工作,但我需要异步功能。

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.core.task.TaskExecutor] is defined: expected single matching bean but found 4: clientOutboundChannelExecutor,messageBrokerTaskScheduler,clientInboundChannelExecutor,brokerChannelExecutor
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:366)
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor.setBeanFactory(AsyncAnnotationBeanPostProcessor.java:128)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1597)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1565)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201)
org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)
org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:682)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:539)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

最佳答案

在 Spring 应用上下文配置中添加 bean

@Configuration
@EnableAsync
public class AppContext extends WebMvcConfigurationSupport {
@Bean
public Executor taskExecutor() {
return new SimpleAsyncTaskExecutor();
}
}

我建议你引用linuxism.tistory.com/2076

如果您在 XML 中声明您的执行程序,那么您可以创建一个类并将其命名为 TaskExecutor。然后当 Spring 尝试查找 TaskExecutor bean 时,它会找到这个。

@Component
public class TaskExecutor extends SimpleAsyncTaskExecutor {
}

关于java - 从 4.2.0.RC3 升级到 4.2.0.RELEASE 时出现 Spring Async 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com