Reorder WebMvcConfigurer from auto-configuration

Prior to this commit, all `WebMvcConfigurer` instances provided by user
configuration were processed *before* the one provided by the
`WebMvcAutoConfiguration`.

For many options this has no consequence, but for some, like the
`ContentNegotiationConfigurer`, settings were overriden by the
auto-configuration even if developers provided an opinion.

This commit orders the `WebMvcConfigurer` provided by the
auto-configuration at `0`, so that custom configurers (unordered, at
`Ordered.LOWEST_PRECEDENCE`) are processed *after*.

This still gives room to developers for configuring things *before* the
auto-configuration - they can still order their own configuration
accordingly.

Fixes gh-12389
pull/12428/head
Brian Clozel 7 years ago
parent 9b1003d9f6
commit 72afdc676d

@ -66,6 +66,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.ClassPathResource;
@ -168,6 +169,7 @@ public class WebMvcAutoConfiguration {
@Configuration
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter
implements WebMvcConfigurer, ResourceLoaderAware {

@ -71,6 +71,7 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
@ -809,6 +810,18 @@ public class WebMvcAutoConfigurationTests {
});
}
@Test
public void customConfigurerAppliedAfterAutoConfig() {
this.contextRunner
.withUserConfiguration(CustomConfigurer.class)
.run((context) -> {
ContentNegotiationManager manager = context.getBean(ContentNegotiationManager.class);
assertThat(manager.getStrategies()).anyMatch(strategy ->
WebMvcAutoConfiguration.OptionalPathExtensionContentNegotiationStrategy.class
.isAssignableFrom(strategy.getClass()));
});
}
private void assertCacheControl(AssertableWebApplicationContext context) {
Map<String, Object> handlerMap = getHandlerMap(
context.getBean("resourceHandlerMapping", HandlerMapping.class));
@ -1086,4 +1099,13 @@ public class WebMvcAutoConfigurationTests {
}
@Configuration
static class CustomConfigurer implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true);
}
}
}

Loading…
Cancel
Save