From 1356bd4359ee74a5c93630349d76ced142b994d9 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 10 Oct 2017 22:35:37 +0200 Subject: [PATCH] Share HandlerInterceptors in ResourceHandlerMapping Prior to this commit, the Resource `HandlerMapping` configured by `@EnableSpringMvc` would be set up with only one custom `HandlerInterceptor` (the one exposing the `ResourceUrlProvider` for resolving URLs of static resources). This behavior is not consistent with the rest of the default configuration which leverages the interceptors configured against the interceptor registry (with possible user-provided ones). This change allows to register interceptors against resource handling; this can be useful for metrics purposes, as in spring-projects/spring-boot#10335. Issue: SPR-16034 --- .../config/annotation/WebMvcConfigurationSupport.java | 2 +- .../WebMvcConfigurationSupportExtensionTests.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 7b726f861c..d8eb43f8aa 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -483,7 +483,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv if (handlerMapping != null) { handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); - handlerMapping.setInterceptors(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())); + handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setCorsConfigurations(getCorsConfigurations()); } else { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java index 556b7d2f55..fa9e85e709 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java @@ -160,9 +160,11 @@ public class WebMvcConfigurationSupportExtensionTests { chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif")); assertNotNull(chain); assertNotNull(chain.getHandler()); - assertEquals(Arrays.toString(chain.getInterceptors()), 2, chain.getInterceptors().length); + assertEquals(Arrays.toString(chain.getInterceptors()), 4, chain.getInterceptors().length); // PathExposingHandlerInterceptor at chain.getInterceptors()[0] - assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[1].getClass()); + assertEquals(LocaleChangeInterceptor.class, chain.getInterceptors()[1].getClass()); + assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[2].getClass()); + assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[3].getClass()); handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping(); handlerMapping.setApplicationContext(this.context);