Skip to content

Commit 3e390d1

Browse files
committed
Declare ResourceUrlProvider in MVC namespace
This change adds a ResourceUrlProvider bean to the ResourceBeanDefinitionParser to match the same in the Java config. For consistency the name of the bean in the Java config is renamed. Also a ResourceUrlProviderExposingInterceptor is declares as a global MappedInterceptor.
1 parent d75f128 commit 3e390d1

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/ResourcesBeanDefinitionParser.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.Arrays;
2020
import java.util.Map;
2121

22+
import org.springframework.web.servlet.handler.MappedInterceptor;
23+
import org.springframework.web.servlet.resource.ResourceUrlProvider;
24+
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
2225
import org.w3c.dom.Element;
2326

2427
import org.springframework.beans.factory.config.BeanDefinition;
@@ -71,11 +74,15 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
7174

7275
private static final String CONTENT_VERSION_STRATEGY_ELEMENT = "content-version-strategy";
7376

77+
private static final String RESOURCE_URL_PROVIDER = "mvcResourceUrlProvider";
78+
7479

7580
@Override
7681
public BeanDefinition parse(Element element, ParserContext parserContext) {
7782
Object source = parserContext.extractSource(element);
7883

84+
registerUrlProvider(parserContext, source);
85+
7986
String resourceHandlerName = registerResourceHandler(parserContext, element, source);
8087
if (resourceHandlerName == null) {
8188
return null;
@@ -113,6 +120,28 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
113120
return null;
114121
}
115122

123+
private void registerUrlProvider(ParserContext parserContext, Object source) {
124+
if (!parserContext.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
125+
RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
126+
urlProvider.setSource(source);
127+
urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
128+
parserContext.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
129+
parserContext.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));
130+
131+
RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
132+
interceptor.setSource(source);
133+
interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);
134+
135+
RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
136+
mappedInterceptor.setSource(source);
137+
mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
138+
mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
139+
mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
140+
String mappedInterceptorName = parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptor);
141+
parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
142+
}
143+
}
144+
116145
private String registerResourceHandler(ParserContext parserContext, Element element, Object source) {
117146
String locationAttr = element.getAttribute("location");
118147
if (!StringUtils.hasText(locationAttr)) {

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected final Object[] getInterceptors() {
269269
InterceptorRegistry registry = new InterceptorRegistry();
270270
addInterceptors(registry);
271271
registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
272-
registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(resourceUrlPathTranslator()));
272+
registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
273273
this.interceptors = registry.getInterceptors();
274274
}
275275
return this.interceptors.toArray();
@@ -387,17 +387,17 @@ protected void addResourceHandlers(ResourceHandlerRegistry registry) {
387387
}
388388

389389
@Bean
390-
public ResourceUrlProvider resourceUrlPathTranslator() {
391-
ResourceUrlProvider translator = new ResourceUrlProvider();
390+
public ResourceUrlProvider mvcResourceUrlProvider() {
391+
ResourceUrlProvider urlProvider = new ResourceUrlProvider();
392392
UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
393393
if (pathHelper != null) {
394-
translator.setUrlPathHelper(pathHelper);
394+
urlProvider.setUrlPathHelper(pathHelper);
395395
}
396396
PathMatcher pathMatcher = getPathMatchConfigurer().getPathMatcher();
397397
if (pathMatcher != null) {
398-
translator.setPathMatcher(pathMatcher);
398+
urlProvider.setPathMatcher(pathMatcher);
399399
}
400-
return translator;
400+
return urlProvider;
401401
}
402402

403403
/**

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
9494
import org.springframework.web.servlet.resource.ResourceResolver;
9595
import org.springframework.web.servlet.resource.ResourceTransformer;
96+
import org.springframework.web.servlet.resource.ResourceUrlProvider;
97+
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
9698
import org.springframework.web.servlet.resource.VersionResourceResolver;
9799
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
98100
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
@@ -288,7 +290,7 @@ public void testInterceptors() throws Exception {
288290

289291
@Test
290292
public void testResources() throws Exception {
291-
loadBeanDefinitions("mvc-config-resources.xml", 7);
293+
loadBeanDefinitions("mvc-config-resources.xml", 9);
292294

293295
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
294296
assertNotNull(adapter);
@@ -304,6 +306,13 @@ public void testResources() throws Exception {
304306
assertNotNull(beanNameMapping);
305307
assertEquals(2, beanNameMapping.getOrder());
306308

309+
ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
310+
assertNotNull(urlProvider);
311+
312+
MappedInterceptor mappedInterceptor = appContext.getBean(MappedInterceptor.class);
313+
assertNotNull(urlProvider);
314+
assertEquals(ResourceUrlProviderExposingInterceptor.class, mappedInterceptor.getInterceptor().getClass());
315+
307316
MockHttpServletRequest request = new MockHttpServletRequest();
308317
request.setRequestURI("/resources/foo.css");
309318
request.setMethod("GET");
@@ -321,7 +330,7 @@ public void testResources() throws Exception {
321330

322331
@Test
323332
public void testResourcesWithOptionalAttributes() throws Exception {
324-
loadBeanDefinitions("mvc-config-resources-optional-attrs.xml", 7);
333+
loadBeanDefinitions("mvc-config-resources-optional-attrs.xml", 9);
325334

326335
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
327336
assertNotNull(mapping);
@@ -336,7 +345,7 @@ public void testResourcesWithOptionalAttributes() throws Exception {
336345

337346
@Test
338347
public void testResourcesWithResolversTransformers() throws Exception {
339-
loadBeanDefinitions("mvc-config-resources-chain.xml", 8);
348+
loadBeanDefinitions("mvc-config-resources-chain.xml", 10);
340349

341350
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
342351
assertNotNull(mapping);
@@ -374,7 +383,7 @@ public void testResourcesWithResolversTransformers() throws Exception {
374383

375384
@Test
376385
public void testResourcesWithResolversTransformersCustom() throws Exception {
377-
loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml", 9);
386+
loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml", 11);
378387

379388
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
380389
assertNotNull(mapping);
@@ -743,7 +752,7 @@ public void testViewResolutionWithContentNegotiation() throws Exception {
743752

744753
@Test
745754
public void testPathMatchingHandlerMappings() throws Exception {
746-
loadBeanDefinitions("mvc-config-path-matching-mappings.xml", 20);
755+
loadBeanDefinitions("mvc-config-path-matching-mappings.xml", 22);
747756

748757
RequestMappingHandlerMapping requestMapping = appContext.getBean(RequestMappingHandlerMapping.class);
749758
assertNotNull(requestMapping);

0 commit comments

Comments
 (0)