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
pull/25409/head
Andy Wilkinson 4 years ago
parent 76e42ff96f
commit 2a2daae14e

@ -49,6 +49,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionService; import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
@ -133,6 +134,7 @@ public class WebFluxAutoConfiguration {
@EnableConfigurationProperties({ org.springframework.boot.autoconfigure.web.ResourceProperties.class, @EnableConfigurationProperties({ org.springframework.boot.autoconfigure.web.ResourceProperties.class,
WebProperties.class, WebFluxProperties.class }) WebProperties.class, WebFluxProperties.class })
@Import({ EnableWebFluxConfiguration.class }) @Import({ EnableWebFluxConfiguration.class })
@Order(0)
public static class WebFluxConfig implements WebFluxConfigurer { public static class WebFluxConfig implements WebFluxConfigurer {
private static final Log logger = LogFactory.getLog(WebFluxConfig.class); private static final Log logger = LogFactory.getLog(WebFluxConfig.class);

@ -39,6 +39,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; 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.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter; 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.filter.reactive.HiddenHttpMethodFilter;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver; 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.WebFluxConfigurationSupport;
import org.springframework.web.reactive.config.WebFluxConfigurer; import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.server.support.RouterFunctionMapping; 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<PathPattern, Object> getHandlerMap(ApplicationContext context) { private Map<PathPattern, Object> getHandlerMap(ApplicationContext context) {
HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class); HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class);
if (mapping instanceof SimpleUrlHandlerMapping) { if (mapping instanceof SimpleUrlHandlerMapping) {
@ -787,4 +800,14 @@ class WebFluxAutoConfigurationTests {
} }
@Order(-100)
static class HighPrecedenceConfigurer implements WebFluxConfigurer {
}
@Order(100)
static class LowPrecedenceConfigurer implements WebFluxConfigurer {
}
} }

Loading…
Cancel
Save