Remove no-op classes in web-related Java config

This commit is contained in:
Rossen Stoyanchev
2018-07-10 17:22:12 -04:00
parent 3d6e38bb43
commit a40d25a760
4 changed files with 37 additions and 99 deletions

View File

@@ -24,7 +24,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactoryUtils;
@@ -448,12 +447,15 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* {@link #addViewControllers}.
*/
@Bean
@Nullable
public HandlerMapping viewControllerHandlerMapping() {
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
addViewControllers(registry);
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
if (handlerMapping == null) {
return null;
}
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
@@ -487,6 +489,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* {@link #addResourceHandlers}.
*/
@Bean
@Nullable
public HandlerMapping resourceHandlerMapping() {
Assert.state(this.applicationContext != null, "No ApplicationContext set");
Assert.state(this.servletContext != null, "No ServletContext set");
@@ -496,15 +499,13 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
addResourceHandlers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
if (handlerMapping != null) {
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setCorsConfigurations(getCorsConfigurations());
}
else {
handlerMapping = new EmptyHandlerMapping();
if (handlerMapping == null) {
return null;
}
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setCorsConfigurations(getCorsConfigurations());
return handlerMapping;
}
@@ -539,13 +540,12 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* override {@link #configureDefaultServletHandling}.
*/
@Bean
@Nullable
public HandlerMapping defaultServletHandlerMapping() {
Assert.state(this.servletContext != null, "No ServletContext set");
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
configureDefaultServletHandling(configurer);
HandlerMapping handlerMapping = configurer.buildHandlerMapping();
return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
return configurer.buildHandlerMapping();
}
/**
@@ -645,7 +645,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
}
/**
* Override this method to add custom {@link Converter}s and {@link Formatter Converter}s and {@link Formatters}.
* Override this method to add custom {@link Converter}s and
* {@link Formatter Converter}s and {@link Formatters}.
*/
protected void addFormatters(FormatterRegistry registry) {
}
@@ -1046,16 +1047,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
}
private static final class EmptyHandlerMapping extends AbstractHandlerMapping {
@Override
protected Object getHandlerInternal(HttpServletRequest request) {
return null;
}
}
private static final class NoOpValidator implements Validator {
@Override

View File

@@ -67,7 +67,6 @@ import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite;
@@ -85,13 +84,9 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.ViewResolverComposite;
import org.springframework.web.util.UrlPathHelper;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
import static com.fasterxml.jackson.databind.MapperFeature.*;
import static org.junit.Assert.*;
/**
* Integration tests for {@link WebMvcConfigurationSupport} (imported via
@@ -123,14 +118,17 @@ public class WebMvcConfigurationSupportTests {
}
@Test
public void emptyViewControllerHandlerMapping() {
public void emptyHandlerMappings() {
ApplicationContext context = initContext(WebConfig.class);
String name = "viewControllerHandlerMapping";
AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
assertNotNull(handlerMapping);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
Object nullBean = context.getBean("viewControllerHandlerMapping");
assertTrue(nullBean.equals(null));
nullBean = context.getBean("resourceHandlerMapping");
assertTrue(nullBean.equals(null));
nullBean = context.getBean("defaultServletHandlerMapping");
assertTrue(nullBean.equals(null));
}
@Test
@@ -149,27 +147,6 @@ public class WebMvcConfigurationSupportTests {
assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
@Test
public void emptyResourceHandlerMapping() {
ApplicationContext context = initContext(WebConfig.class);
AbstractHandlerMapping handlerMapping = context.getBean("resourceHandlerMapping", AbstractHandlerMapping.class);
assertNotNull(handlerMapping);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
}
@Test
public void emptyDefaultServletHandlerMapping() {
ApplicationContext context = initContext(WebConfig.class);
String name = "defaultServletHandlerMapping";
AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
assertNotNull(handlerMapping);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
}
@Test
public void requestMappingHandlerAdapter() throws Exception {
ApplicationContext context = initContext(WebConfig.class);