From 32faf09a8069ee33822216317d5965db2d1e9064 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 24 Jul 2018 20:45:48 -0400 Subject: [PATCH] Add check for mixing @EnableWebMvc and @EnableWebFlux Issue: SPR-16609 --- .../config/WebFluxConfigurationSupport.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java index c70e84faef..ed55b0988c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java @@ -24,6 +24,7 @@ import reactor.core.publisher.Mono; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; @@ -38,6 +39,7 @@ import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.FormattingConversionService; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.validation.Errors; import org.springframework.validation.MessageCodesResolver; @@ -93,6 +95,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware { @Override public void setApplicationContext(@Nullable ApplicationContext applicationContext) { this.applicationContext = applicationContext; + assertWebMvcNotEnabled(applicationContext); } @Nullable @@ -100,6 +103,19 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware { return this.applicationContext; } + private static void assertWebMvcNotEnabled(@Nullable ApplicationContext applicationContext) { + try { + if (applicationContext != null) { + Assert.isNull(applicationContext.getType("mvcContentNegotiationManager"), + "The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, " + + "e.g. via @EnableWebMvc and @EnableWebFlux, in the same application."); + } + } + catch (NoSuchBeanDefinitionException ex) { + // Expected... + } + } + @Bean public DispatcherHandler webHandler() {