Remove factory methods from WebReactiveConfigurer
This commit removes factory methods for `RequestMappingHandlerMapping` and `RequestMappingHandlerAdapter` from `WebReactiveConfigurer`. `WebReactiveConfigurer` should promote only common customizations; those factory methods should be overridden when extending `WebReactiveConfigurationSupport`, in advanced configuration scenarios.
This commit is contained in:
@@ -28,8 +28,6 @@ import org.springframework.validation.MessageCodesResolver;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* A subclass of {@code WebReactiveConfigurationSupport} that detects and delegates
|
||||
@@ -52,12 +50,6 @@ public class DelegatingWebReactiveConfiguration extends WebReactiveConfiguration
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
|
||||
return this.configurers.createRequestMappingHandlerMapping()
|
||||
.orElse(super.createRequestMappingHandlerMapping());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureRequestedContentTypeResolver(RequestedContentTypeResolverBuilder builder) {
|
||||
this.configurers.configureRequestedContentTypeResolver(builder);
|
||||
@@ -78,12 +70,6 @@ public class DelegatingWebReactiveConfiguration extends WebReactiveConfiguration
|
||||
this.configurers.addResourceHandlers(registry);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() {
|
||||
return this.configurers.createRequestMappingHandlerAdapter()
|
||||
.orElse(super.createRequestMappingHandlerAdapter());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
this.configurers.addArgumentResolvers(resolvers);
|
||||
|
||||
@@ -30,8 +30,6 @@ import org.springframework.web.reactive.accept.CompositeContentTypeResolver;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* Defines callback methods to customize the configuration for Web Reactive
|
||||
@@ -151,24 +149,4 @@ public interface WebReactiveConfigurer {
|
||||
default void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for the {@link RequestMappingHandlerMapping} bean creating
|
||||
* an instance or a custom extension of it. Note that only one configurer
|
||||
* is allowed to implement this method.
|
||||
* The default implementation returns {@code Optional.empty()}.
|
||||
*/
|
||||
default Optional<RequestMappingHandlerMapping> createRequestMappingHandlerMapping() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for the {@link RequestMappingHandlerAdapter} bean creating
|
||||
* an instance or a custom extension of it. Note that only one configurer
|
||||
* is allowed to implement this method.
|
||||
* The default implementation returns {@code Optional.empty()}.
|
||||
*/
|
||||
default Optional<RequestMappingHandlerAdapter> createRequestMappingHandlerAdapter() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,8 +30,6 @@ import org.springframework.validation.MessageCodesResolver;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* A {@link WebReactiveConfigurer} that delegates to one or more others.
|
||||
@@ -117,18 +115,6 @@ public class WebReactiveConfigurerComposite implements WebReactiveConfigurer {
|
||||
this.delegates.stream().forEach(delegate -> delegate.configureViewResolvers(registry));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<RequestMappingHandlerMapping> createRequestMappingHandlerMapping() {
|
||||
return createSingleBean(WebReactiveConfigurer::createRequestMappingHandlerMapping,
|
||||
RequestMappingHandlerMapping.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<RequestMappingHandlerAdapter> createRequestMappingHandlerAdapter() {
|
||||
return createSingleBean(WebReactiveConfigurer::createRequestMappingHandlerAdapter,
|
||||
RequestMappingHandlerAdapter.class);
|
||||
}
|
||||
|
||||
private <T> Optional<T> createSingleBean(Function<WebReactiveConfigurer, Optional<T>> factory,
|
||||
Class<T> beanType) {
|
||||
|
||||
|
||||
@@ -16,15 +16,12 @@
|
||||
|
||||
package org.springframework.web.reactive.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
@@ -39,17 +36,12 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link DelegatingWebReactiveConfiguration} tests.
|
||||
@@ -72,17 +64,12 @@ public class DelegatingWebReactiveConfigurationTests {
|
||||
@Captor
|
||||
private ArgumentCaptor<FormatterRegistry> formatterRegistry;
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
delegatingConfig = new DelegatingWebReactiveConfiguration();
|
||||
delegatingConfig.setApplicationContext(new StaticApplicationContext());
|
||||
given(webReactiveConfigurer.createRequestMappingHandlerMapping()).willReturn(Optional.empty());
|
||||
given(webReactiveConfigurer.createRequestMappingHandlerAdapter()).willReturn(Optional.empty());
|
||||
given(webReactiveConfigurer.getValidator()).willReturn(Optional.empty());
|
||||
given(webReactiveConfigurer.getMessageCodesResolver()).willReturn(Optional.empty());
|
||||
}
|
||||
@@ -93,46 +80,11 @@ public class DelegatingWebReactiveConfigurationTests {
|
||||
delegatingConfig.setConfigurers(Collections.singletonList(webReactiveConfigurer));
|
||||
delegatingConfig.requestMappingHandlerMapping();
|
||||
|
||||
verify(webReactiveConfigurer).createRequestMappingHandlerMapping();
|
||||
verify(webReactiveConfigurer).configureRequestedContentTypeResolver(any(RequestedContentTypeResolverBuilder.class));
|
||||
verify(webReactiveConfigurer).addCorsMappings(any(CorsRegistry.class));
|
||||
verify(webReactiveConfigurer).configurePathMatching(any(PathMatchConfigurer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestMappingHandlerMappingFactoryMethod() throws Exception {
|
||||
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
|
||||
|
||||
WebReactiveConfigurer configurer1 = mock(WebReactiveConfigurer.class);
|
||||
WebReactiveConfigurer configurer2 = mock(WebReactiveConfigurer.class);
|
||||
|
||||
when(configurer1.createRequestMappingHandlerMapping()).thenReturn(Optional.of(mapping));
|
||||
when(configurer2.createRequestMappingHandlerMapping()).thenReturn(Optional.empty());
|
||||
|
||||
delegatingConfig.setConfigurers(Arrays.asList(configurer1, configurer2));
|
||||
Object actual = delegatingConfig.createRequestMappingHandlerMapping();
|
||||
|
||||
assertSame(mapping, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleRequestMappingHandlerMappingFactoryMethods() throws Exception {
|
||||
RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
|
||||
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
|
||||
|
||||
WebReactiveConfigurer configurer1 = mock(WebReactiveConfigurer.class);
|
||||
WebReactiveConfigurer configurer2 = mock(WebReactiveConfigurer.class);
|
||||
|
||||
when(configurer1.createRequestMappingHandlerMapping()).thenReturn(Optional.of(mapping1));
|
||||
when(configurer2.createRequestMappingHandlerMapping()).thenReturn(Optional.of(mapping2));
|
||||
|
||||
this.thrown.expectMessage("More than one WebReactiveConfigurer implements " +
|
||||
"RequestMappingHandlerMapping factory method.");
|
||||
|
||||
delegatingConfig.setConfigurers(Arrays.asList(configurer1, configurer2));
|
||||
delegatingConfig.createRequestMappingHandlerMapping();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestMappingHandlerAdapter() throws Exception {
|
||||
delegatingConfig.setConfigurers(Collections.singletonList(webReactiveConfigurer));
|
||||
@@ -142,7 +94,6 @@ public class DelegatingWebReactiveConfigurationTests {
|
||||
ConversionService initializerConversionService = initializer.getConversionService();
|
||||
assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
|
||||
|
||||
verify(webReactiveConfigurer).createRequestMappingHandlerAdapter();
|
||||
verify(webReactiveConfigurer).configureMessageReaders(readers.capture());
|
||||
verify(webReactiveConfigurer).extendMessageReaders(readers.capture());
|
||||
verify(webReactiveConfigurer).getValidator();
|
||||
|
||||
Reference in New Issue
Block a user