Skip to content

Commit a40d25a

Browse files
committed
Remove no-op classes in web-related Java config
1 parent 3d6e38b commit a40d25a

File tree

4 files changed

+37
-99
lines changed

4 files changed

+37
-99
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.context.ApplicationContextAware;
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.lang.Nullable;
31-
import org.springframework.messaging.Message;
3231
import org.springframework.messaging.MessageHandler;
3332
import org.springframework.messaging.converter.ByteArrayMessageConverter;
3433
import org.springframework.messaging.converter.CompositeMessageConverter;
@@ -290,10 +289,11 @@ protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> retu
290289
}
291290

292291
@Bean
292+
@Nullable
293293
public AbstractBrokerMessageHandler simpleBrokerMessageHandler() {
294294
SimpleBrokerMessageHandler handler = getBrokerRegistry().getSimpleBroker(brokerChannel());
295295
if (handler == null) {
296-
return new NoOpBrokerMessageHandler();
296+
return null;
297297
}
298298
updateUserDestinationResolver(handler);
299299
return handler;
@@ -307,10 +307,11 @@ private void updateUserDestinationResolver(AbstractBrokerMessageHandler handler)
307307
}
308308

309309
@Bean
310+
@Nullable
310311
public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() {
311312
StompBrokerRelayMessageHandler handler = getBrokerRegistry().getStompBrokerRelay(brokerChannel());
312313
if (handler == null) {
313-
return new NoOpBrokerMessageHandler();
314+
return null;
314315
}
315316
Map<String, MessageHandler> subscriptions = new HashMap<>(1);
316317
String destination = getBrokerRegistry().getUserDestinationBroadcast();
@@ -338,9 +339,10 @@ public UserDestinationMessageHandler userDestinationMessageHandler() {
338339
}
339340

340341
@Bean
342+
@Nullable
341343
public MessageHandler userRegistryMessageHandler() {
342344
if (getBrokerRegistry().getUserRegistryBroadcast() == null) {
343-
return new NoOpMessageHandler();
345+
return null;
344346
}
345347
SimpUserRegistry userRegistry = userRegistry();
346348
Assert.isInstanceOf(MultiServerUserRegistry.class, userRegistry, "MultiServerUserRegistry required");
@@ -424,7 +426,8 @@ public SimpUserRegistry userRegistry() {
424426
protected abstract SimpUserRegistry createLocalUserRegistry();
425427

426428
/**
427-
* Return a {@link org.springframework.validation.Validator org.springframework.validation.Validators} instance for validating
429+
* Return a {@link org.springframework.validation.Validator
430+
* org.springframework.validation.Validators} instance for validating
428431
* {@code @Payload} method arguments.
429432
* <p>In order, this method tries to get a Validator instance:
430433
* <ul>
@@ -477,36 +480,4 @@ public Validator getValidator() {
477480
return null;
478481
}
479482

480-
481-
private static class NoOpMessageHandler implements MessageHandler {
482-
483-
@Override
484-
public void handleMessage(Message<?> message) {
485-
}
486-
}
487-
488-
489-
private class NoOpBrokerMessageHandler extends AbstractBrokerMessageHandler {
490-
491-
public NoOpBrokerMessageHandler() {
492-
super(clientInboundChannel(), clientOutboundChannel(), brokerChannel());
493-
}
494-
495-
@Override
496-
public void start() {
497-
}
498-
499-
@Override
500-
public void stop() {
501-
}
502-
503-
@Override
504-
public void handleMessage(Message<?> message) {
505-
}
506-
507-
@Override
508-
protected void handleMessageInternal(Message<?> message) {
509-
}
510-
}
511-
512483
}

spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,8 @@ public void userBroadcastsDisabledWithSimpleBroker() {
458458
UserDestinationMessageHandler handler = context.getBean(UserDestinationMessageHandler.class);
459459
assertNull(handler.getBroadcastDestination());
460460

461-
String name = "userRegistryMessageHandler";
462-
MessageHandler messageHandler = context.getBean(name, MessageHandler.class);
463-
assertNotEquals(UserRegistryMessageHandler.class, messageHandler.getClass());
461+
Object nullBean = context.getBean("userRegistryMessageHandler");
462+
assertTrue(nullBean.equals(null));
464463
}
465464

466465
@Test // SPR-16275

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525
import java.util.function.Predicate;
2626
import javax.servlet.ServletContext;
27-
import javax.servlet.http.HttpServletRequest;
2827

2928
import org.springframework.beans.BeanUtils;
3029
import org.springframework.beans.factory.BeanFactoryUtils;
@@ -448,12 +447,15 @@ protected void configureContentNegotiation(ContentNegotiationConfigurer configur
448447
* {@link #addViewControllers}.
449448
*/
450449
@Bean
450+
@Nullable
451451
public HandlerMapping viewControllerHandlerMapping() {
452452
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
453453
addViewControllers(registry);
454454

455455
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
456-
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
456+
if (handlerMapping == null) {
457+
return null;
458+
}
457459
handlerMapping.setPathMatcher(mvcPathMatcher());
458460
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
459461
handlerMapping.setInterceptors(getInterceptors());
@@ -487,6 +489,7 @@ public BeanNameUrlHandlerMapping beanNameHandlerMapping() {
487489
* {@link #addResourceHandlers}.
488490
*/
489491
@Bean
492+
@Nullable
490493
public HandlerMapping resourceHandlerMapping() {
491494
Assert.state(this.applicationContext != null, "No ApplicationContext set");
492495
Assert.state(this.servletContext != null, "No ServletContext set");
@@ -496,15 +499,13 @@ public HandlerMapping resourceHandlerMapping() {
496499
addResourceHandlers(registry);
497500

498501
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
499-
if (handlerMapping != null) {
500-
handlerMapping.setPathMatcher(mvcPathMatcher());
501-
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
502-
handlerMapping.setInterceptors(getInterceptors());
503-
handlerMapping.setCorsConfigurations(getCorsConfigurations());
504-
}
505-
else {
506-
handlerMapping = new EmptyHandlerMapping();
502+
if (handlerMapping == null) {
503+
return null;
507504
}
505+
handlerMapping.setPathMatcher(mvcPathMatcher());
506+
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
507+
handlerMapping.setInterceptors(getInterceptors());
508+
handlerMapping.setCorsConfigurations(getCorsConfigurations());
508509
return handlerMapping;
509510
}
510511

@@ -539,13 +540,12 @@ public ResourceUrlProvider mvcResourceUrlProvider() {
539540
* override {@link #configureDefaultServletHandling}.
540541
*/
541542
@Bean
543+
@Nullable
542544
public HandlerMapping defaultServletHandlerMapping() {
543545
Assert.state(this.servletContext != null, "No ServletContext set");
544546
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
545547
configureDefaultServletHandling(configurer);
546-
547-
HandlerMapping handlerMapping = configurer.buildHandlerMapping();
548-
return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
548+
return configurer.buildHandlerMapping();
549549
}
550550

551551
/**
@@ -645,7 +645,8 @@ public FormattingConversionService mvcConversionService() {
645645
}
646646

647647
/**
648-
* Override this method to add custom {@link Converter}s and {@link Formatter Converter}s and {@link Formatters}.
648+
* Override this method to add custom {@link Converter}s and
649+
* {@link Formatter Converter}s and {@link Formatters}.
649650
*/
650651
protected void addFormatters(FormatterRegistry registry) {
651652
}
@@ -1046,16 +1047,6 @@ public HandlerMappingIntrospector mvcHandlerMappingIntrospector() {
10461047
}
10471048

10481049

1049-
1050-
private static final class EmptyHandlerMapping extends AbstractHandlerMapping {
1051-
1052-
@Override
1053-
protected Object getHandlerInternal(HttpServletRequest request) {
1054-
return null;
1055-
}
1056-
}
1057-
1058-
10591050
private static final class NoOpValidator implements Validator {
10601051

10611052
@Override

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import org.springframework.web.servlet.HandlerExceptionResolver;
6868
import org.springframework.web.servlet.HandlerExecutionChain;
6969
import org.springframework.web.servlet.ViewResolver;
70-
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
7170
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
7271
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
7372
import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite;
@@ -85,13 +84,9 @@
8584
import org.springframework.web.servlet.view.ViewResolverComposite;
8685
import org.springframework.web.util.UrlPathHelper;
8786

88-
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
89-
import static com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION;
90-
import static org.junit.Assert.assertEquals;
91-
import static org.junit.Assert.assertFalse;
92-
import static org.junit.Assert.assertNotNull;
93-
import static org.junit.Assert.assertNull;
94-
import static org.junit.Assert.assertTrue;
87+
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
88+
import static com.fasterxml.jackson.databind.MapperFeature.*;
89+
import static org.junit.Assert.*;
9590

9691
/**
9792
* Integration tests for {@link WebMvcConfigurationSupport} (imported via
@@ -123,14 +118,17 @@ public void requestMappingHandlerMapping() throws Exception {
123118
}
124119

125120
@Test
126-
public void emptyViewControllerHandlerMapping() {
121+
public void emptyHandlerMappings() {
127122
ApplicationContext context = initContext(WebConfig.class);
128-
String name = "viewControllerHandlerMapping";
129-
AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
130123

131-
assertNotNull(handlerMapping);
132-
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
133-
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
124+
Object nullBean = context.getBean("viewControllerHandlerMapping");
125+
assertTrue(nullBean.equals(null));
126+
127+
nullBean = context.getBean("resourceHandlerMapping");
128+
assertTrue(nullBean.equals(null));
129+
130+
nullBean = context.getBean("defaultServletHandlerMapping");
131+
assertTrue(nullBean.equals(null));
134132
}
135133

136134
@Test
@@ -149,27 +147,6 @@ public void beanNameHandlerMapping() throws Exception {
149147
assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
150148
}
151149

152-
@Test
153-
public void emptyResourceHandlerMapping() {
154-
ApplicationContext context = initContext(WebConfig.class);
155-
AbstractHandlerMapping handlerMapping = context.getBean("resourceHandlerMapping", AbstractHandlerMapping.class);
156-
157-
assertNotNull(handlerMapping);
158-
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
159-
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
160-
}
161-
162-
@Test
163-
public void emptyDefaultServletHandlerMapping() {
164-
ApplicationContext context = initContext(WebConfig.class);
165-
String name = "defaultServletHandlerMapping";
166-
AbstractHandlerMapping handlerMapping = context.getBean(name, AbstractHandlerMapping.class);
167-
168-
assertNotNull(handlerMapping);
169-
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
170-
assertTrue(handlerMapping.getClass().getName().endsWith("EmptyHandlerMapping"));
171-
}
172-
173150
@Test
174151
public void requestMappingHandlerAdapter() throws Exception {
175152
ApplicationContext context = initContext(WebConfig.class);

0 commit comments

Comments
 (0)