我有一个 Spring Webflux 应用程序,我试图从旧模块加载依赖项(旧模块位于 Spring WebMVC 框架上)。
当应用程序启动时,抛出此错误 -
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'requestMappingHandlerAdapter', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
我希望启动 webflux 包中的所有 bean,因此无法设置 spring.main.allow-bean-definition-overriding=true
。
还尝试在组件扫描时排除 org.springframework.boot 中的所有类 - @ComponentScan(excludeFilters = @Filter(type = FilterType.REGEX,pattern = "org.springframework.boot*")
。
还尝试排除我的 webflux 项目的 pom.xml 中的所有 spring 包,如下所示 -
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
由于我无法将旧的依赖项项目修改为 webflux,因此我可以使用任何选项来使代码正常工作吗?
最佳答案
在 Spring Boot 启动类中,@EnableAutoConfiguration
注解将自动配置 MVC 部分(WebMvcAutoConfiguration
将因 DelegatingWebFluxConfiguration
中的相同 bean 名称而失败)
因此尝试将其从自动配置中排除,如下所示:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {WebMvcAutoConfiguration.class })
public static void main(String[] args) {
...
SpringApplication.run(MyApp.class, args);
}
关于java - 由于相同的 bean,应用程序无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57472586/