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

@@ -28,7 +28,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.converter.ByteArrayMessageConverter; import org.springframework.messaging.converter.ByteArrayMessageConverter;
import org.springframework.messaging.converter.CompositeMessageConverter; import org.springframework.messaging.converter.CompositeMessageConverter;
@@ -290,10 +289,11 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
@Bean @Bean
@Nullable
public AbstractBrokerMessageHandler simpleBrokerMessageHandler() { public AbstractBrokerMessageHandler simpleBrokerMessageHandler() {
SimpleBrokerMessageHandler handler = getBrokerRegistry().getSimpleBroker(brokerChannel()); SimpleBrokerMessageHandler handler = getBrokerRegistry().getSimpleBroker(brokerChannel());
if (handler == null) { if (handler == null) {
return new NoOpBrokerMessageHandler(); return null;
} }
updateUserDestinationResolver(handler); updateUserDestinationResolver(handler);
return handler; return handler;
@@ -307,10 +307,11 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
@Bean @Bean
@Nullable
public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() { public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() {
StompBrokerRelayMessageHandler handler = getBrokerRegistry().getStompBrokerRelay(brokerChannel()); StompBrokerRelayMessageHandler handler = getBrokerRegistry().getStompBrokerRelay(brokerChannel());
if (handler == null) { if (handler == null) {
return new NoOpBrokerMessageHandler(); return null;
} }
Map<String, MessageHandler> subscriptions = new HashMap<>(1); Map<String, MessageHandler> subscriptions = new HashMap<>(1);
String destination = getBrokerRegistry().getUserDestinationBroadcast(); String destination = getBrokerRegistry().getUserDestinationBroadcast();
@@ -338,9 +339,10 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
} }
@Bean @Bean
@Nullable
public MessageHandler userRegistryMessageHandler() { public MessageHandler userRegistryMessageHandler() {
if (getBrokerRegistry().getUserRegistryBroadcast() == null) { if (getBrokerRegistry().getUserRegistryBroadcast() == null) {
return new NoOpMessageHandler(); return null;
} }
SimpUserRegistry userRegistry = userRegistry(); SimpUserRegistry userRegistry = userRegistry();
Assert.isInstanceOf(MultiServerUserRegistry.class, userRegistry, "MultiServerUserRegistry required"); Assert.isInstanceOf(MultiServerUserRegistry.class, userRegistry, "MultiServerUserRegistry required");
@@ -424,7 +426,8 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
protected abstract SimpUserRegistry createLocalUserRegistry(); protected abstract SimpUserRegistry createLocalUserRegistry();
/** /**
* Return a {@link org.springframework.validation.Validator org.springframework.validation.Validators} instance for validating * Return a {@link org.springframework.validation.Validator
* org.springframework.validation.Validators} instance for validating
* {@code @Payload} method arguments. * {@code @Payload} method arguments.
* <p>In order, this method tries to get a Validator instance: * <p>In order, this method tries to get a Validator instance:
* <ul> * <ul>
@@ -477,36 +480,4 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
return null; return null;
} }
private static class NoOpMessageHandler implements MessageHandler {
@Override
public void handleMessage(Message<?> message) {
}
}
private class NoOpBrokerMessageHandler extends AbstractBrokerMessageHandler {
public NoOpBrokerMessageHandler() {
super(clientInboundChannel(), clientOutboundChannel(), brokerChannel());
}
@Override
public void start() {
}
@Override
public void stop() {
}
@Override
public void handleMessage(Message<?> message) {
}
@Override
protected void handleMessageInternal(Message<?> message) {
}
}
} }

View File

@@ -458,9 +458,8 @@ public class MessageBrokerConfigurationTests {
UserDestinationMessageHandler handler = context.getBean(UserDestinationMessageHandler.class); UserDestinationMessageHandler handler = context.getBean(UserDestinationMessageHandler.class);
assertNull(handler.getBroadcastDestination()); assertNull(handler.getBroadcastDestination());
String name = "userRegistryMessageHandler"; Object nullBean = context.getBean("userRegistryMessageHandler");
MessageHandler messageHandler = context.getBean(name, MessageHandler.class); assertTrue(nullBean.equals(null));
assertNotEquals(UserRegistryMessageHandler.class, messageHandler.getClass());
} }
@Test // SPR-16275 @Test // SPR-16275

View File

@@ -24,7 +24,6 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate; import java.util.function.Predicate;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
@@ -448,12 +447,15 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* {@link #addViewControllers}. * {@link #addViewControllers}.
*/ */
@Bean @Bean
@Nullable
public HandlerMapping viewControllerHandlerMapping() { public HandlerMapping viewControllerHandlerMapping() {
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext); ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
addViewControllers(registry); addViewControllers(registry);
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping(); AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping()); if (handlerMapping == null) {
return null;
}
handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setInterceptors(getInterceptors());
@@ -487,6 +489,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* {@link #addResourceHandlers}. * {@link #addResourceHandlers}.
*/ */
@Bean @Bean
@Nullable
public HandlerMapping resourceHandlerMapping() { public HandlerMapping resourceHandlerMapping() {
Assert.state(this.applicationContext != null, "No ApplicationContext set"); Assert.state(this.applicationContext != null, "No ApplicationContext set");
Assert.state(this.servletContext != null, "No ServletContext set"); Assert.state(this.servletContext != null, "No ServletContext set");
@@ -496,15 +499,13 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
addResourceHandlers(registry); addResourceHandlers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
if (handlerMapping != null) { if (handlerMapping == null) {
handlerMapping.setPathMatcher(mvcPathMatcher()); return null;
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setCorsConfigurations(getCorsConfigurations());
}
else {
handlerMapping = new EmptyHandlerMapping();
} }
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setCorsConfigurations(getCorsConfigurations());
return handlerMapping; return handlerMapping;
} }
@@ -539,13 +540,12 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* override {@link #configureDefaultServletHandling}. * override {@link #configureDefaultServletHandling}.
*/ */
@Bean @Bean
@Nullable
public HandlerMapping defaultServletHandlerMapping() { public HandlerMapping defaultServletHandlerMapping() {
Assert.state(this.servletContext != null, "No ServletContext set"); Assert.state(this.servletContext != null, "No ServletContext set");
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext); DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
configureDefaultServletHandling(configurer); configureDefaultServletHandling(configurer);
return configurer.buildHandlerMapping();
HandlerMapping handlerMapping = configurer.buildHandlerMapping();
return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
} }
/** /**
@@ -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) { 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 { private static final class NoOpValidator implements Validator {
@Override @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.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.ViewResolver; 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.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor; import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite; 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.servlet.view.ViewResolverComposite;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; import static com.fasterxml.jackson.databind.DeserializationFeature.*;
import static com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION; import static com.fasterxml.jackson.databind.MapperFeature.*;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/** /**
* Integration tests for {@link WebMvcConfigurationSupport} (imported via * Integration tests for {@link WebMvcConfigurationSupport} (imported via
@@ -123,14 +118,17 @@ public class WebMvcConfigurationSupportTests {
} }
@Test @Test
public void emptyViewControllerHandlerMapping() { public void emptyHandlerMappings() {
ApplicationContext context = initContext(WebConfig.class); ApplicationContext context = initContext(WebConfig.class);
String name = "viewControllerHandlerMapping";
AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
assertNotNull(handlerMapping); Object nullBean = context.getBean("viewControllerHandlerMapping");
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder()); assertTrue(nullBean.equals(null));
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
nullBean = context.getBean("resourceHandlerMapping");
assertTrue(nullBean.equals(null));
nullBean = context.getBean("defaultServletHandlerMapping");
assertTrue(nullBean.equals(null));
} }
@Test @Test
@@ -149,27 +147,6 @@ public class WebMvcConfigurationSupportTests {
assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass()); 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 @Test
public void requestMappingHandlerAdapter() throws Exception { public void requestMappingHandlerAdapter() throws Exception {
ApplicationContext context = initContext(WebConfig.class); ApplicationContext context = initContext(WebConfig.class);