From 2a2daae14e5e0042ebfbc55d42d9a83dfe89200d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 24 Feb 2021 10:21:52 +0000 Subject: [PATCH] Allow user's WebFluxConfigurers to be ordered after auto-config's Previously, WebFluxAutoConfiguration's WebFluxConfigurer was unordered. This mean that it had lowest precedence so it was not possible for a user to provide their own configurer that was guaranteed to run after the auto-configuration's configurer. This commit updates the auto-configuration to order its configurer at 0. Any unordered user-defined configurer will now run after the auto-configuration's configurer. Closes gh-25302 --- .../reactive/WebFluxAutoConfiguration.java | 2 ++ .../WebFluxAutoConfigurationTests.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java index 41349b5848..9b201e8b95 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java @@ -49,6 +49,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.format.FormatterRegistry; import org.springframework.format.support.FormattingConversionService; import org.springframework.http.codec.ServerCodecConfigurer; @@ -133,6 +134,7 @@ public class WebFluxAutoConfiguration { @EnableConfigurationProperties({ org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class, WebFluxProperties.class }) @Import({ EnableWebFluxConfiguration.class }) + @Order(0) public static class WebFluxConfig implements WebFluxConfigurer { private static final Log logger = LogFactory.getLog(WebFluxConfig.class); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java index c105b0f328..b04e9eefd6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java @@ -39,6 +39,7 @@ import org.junit.jupiter.params.provider.ValueSource; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; +import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration.WebFluxConfig; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter; @@ -66,6 +67,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.filter.reactive.HiddenHttpMethodFilter; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.accept.RequestedContentTypeResolver; +import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration; import org.springframework.web.reactive.config.WebFluxConfigurationSupport; import org.springframework.web.reactive.config.WebFluxConfigurer; import org.springframework.web.reactive.function.server.support.RouterFunctionMapping; @@ -544,6 +546,17 @@ class WebFluxAutoConfigurationTests { }); } + @Test + @SuppressWarnings("rawtypes") + void userConfigurersCanBeOrderedBeforeOrAfterTheAutoConfiguredConfigurer() { + this.contextRunner.withBean(HighPrecedenceConfigurer.class, HighPrecedenceConfigurer::new) + .withBean(LowPrecedenceConfigurer.class, LowPrecedenceConfigurer::new) + .run((context) -> assertThat(context.getBean(DelegatingWebFluxConfiguration.class)) + .extracting("configurers.delegates").asList() + .extracting((configurer) -> (Class) configurer.getClass()).containsExactly( + HighPrecedenceConfigurer.class, WebFluxConfig.class, LowPrecedenceConfigurer.class)); + } + private Map getHandlerMap(ApplicationContext context) { HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class); if (mapping instanceof SimpleUrlHandlerMapping) { @@ -787,4 +800,14 @@ class WebFluxAutoConfigurationTests { } + @Order(-100) + static class HighPrecedenceConfigurer implements WebFluxConfigurer { + + } + + @Order(100) + static class LowPrecedenceConfigurer implements WebFluxConfigurer { + + } + }