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.
This commit is contained in:
@@ -19,6 +19,9 @@ package org.springframework.web.servlet.config;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.servlet.handler.MappedInterceptor;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProvider;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
@@ -71,11 +74,15 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
private static final String CONTENT_VERSION_STRATEGY_ELEMENT = "content-version-strategy";
|
||||
|
||||
private static final String RESOURCE_URL_PROVIDER = "mvcResourceUrlProvider";
|
||||
|
||||
|
||||
@Override
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
Object source = parserContext.extractSource(element);
|
||||
|
||||
registerUrlProvider(parserContext, source);
|
||||
|
||||
String resourceHandlerName = registerResourceHandler(parserContext, element, source);
|
||||
if (resourceHandlerName == null) {
|
||||
return null;
|
||||
@@ -113,6 +120,28 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void registerUrlProvider(ParserContext parserContext, Object source) {
|
||||
if (!parserContext.getRegistry().containsBeanDefinition(RESOURCE_URL_PROVIDER)) {
|
||||
RootBeanDefinition urlProvider = new RootBeanDefinition(ResourceUrlProvider.class);
|
||||
urlProvider.setSource(source);
|
||||
urlProvider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
parserContext.getRegistry().registerBeanDefinition(RESOURCE_URL_PROVIDER, urlProvider);
|
||||
parserContext.registerComponent(new BeanComponentDefinition(urlProvider, RESOURCE_URL_PROVIDER));
|
||||
|
||||
RootBeanDefinition interceptor = new RootBeanDefinition(ResourceUrlProviderExposingInterceptor.class);
|
||||
interceptor.setSource(source);
|
||||
interceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, urlProvider);
|
||||
|
||||
RootBeanDefinition mappedInterceptor = new RootBeanDefinition(MappedInterceptor.class);
|
||||
mappedInterceptor.setSource(source);
|
||||
mappedInterceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
|
||||
mappedInterceptor.getConstructorArgumentValues().addIndexedArgumentValue(1, interceptor);
|
||||
String mappedInterceptorName = parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptor);
|
||||
parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptor, mappedInterceptorName));
|
||||
}
|
||||
}
|
||||
|
||||
private String registerResourceHandler(ParserContext parserContext, Element element, Object source) {
|
||||
String locationAttr = element.getAttribute("location");
|
||||
if (!StringUtils.hasText(locationAttr)) {
|
||||
|
||||
@@ -269,7 +269,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
InterceptorRegistry registry = new InterceptorRegistry();
|
||||
addInterceptors(registry);
|
||||
registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
|
||||
registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(resourceUrlPathTranslator()));
|
||||
registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
|
||||
this.interceptors = registry.getInterceptors();
|
||||
}
|
||||
return this.interceptors.toArray();
|
||||
@@ -387,17 +387,17 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceUrlProvider resourceUrlPathTranslator() {
|
||||
ResourceUrlProvider translator = new ResourceUrlProvider();
|
||||
public ResourceUrlProvider mvcResourceUrlProvider() {
|
||||
ResourceUrlProvider urlProvider = new ResourceUrlProvider();
|
||||
UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
|
||||
if (pathHelper != null) {
|
||||
translator.setUrlPathHelper(pathHelper);
|
||||
urlProvider.setUrlPathHelper(pathHelper);
|
||||
}
|
||||
PathMatcher pathMatcher = getPathMatchConfigurer().getPathMatcher();
|
||||
if (pathMatcher != null) {
|
||||
translator.setPathMatcher(pathMatcher);
|
||||
urlProvider.setPathMatcher(pathMatcher);
|
||||
}
|
||||
return translator;
|
||||
return urlProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user