SPR-8430 Rename WebMvcConfiguration to DelegatingWebMvcConfiguration, make it public and make delegation methods final

This commit is contained in:
Rossen Stoyanchev 2011-06-13 12:20:25 +00:00
parent 25e2537c17
commit 883ac319bc
4 changed files with 22 additions and 30 deletions

View File

@ -25,22 +25,15 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerExceptionResolver;
/** /**
* Provides default configuration for Spring MVC applications by registering Spring MVC infrastructure components * A variant of {@link WebMvcConfigurationSupport} that delegates to one or more registered {@link WebMvcConfigurer}
* to be detected by the {@link DispatcherServlet}. This class is imported whenever @{@link EnableWebMvc} is * implementations allowing each of them to customize the default Spring MVC configuration.
* added to an @{@link Configuration} class.
* *
* <p>See the base class {@link WebMvcConfigurationSupport} for a list of registered instances. This class is closed * <p>This class is automatically imported when @{@link EnableWebMvc} is used on an @{@link Configuration} class.
* for extension. However, the configuration it provides can be customized by having your @{@link Configuration} * In turn it detects all implementations of {@link WebMvcConfigurer} via autowiring and in turn delegates to them.
* class implement {@link WebMvcConfigurer} or more conveniently extend from {@link WebMvcConfigurerAdapter}.
* *
* <p>This class will detect your @{@link Configuration} class and any other @{@link Configuration} classes that
* implement {@link WebMvcConfigurer} via autowiring and will allow each of them to participate in the process
* of configuring Spring MVC through the configuration callbacks defined in {@link WebMvcConfigurer}.
*
* @see EnableWebMvc * @see EnableWebMvc
* @see WebMvcConfigurer * @see WebMvcConfigurer
* *
@ -48,7 +41,7 @@ import org.springframework.web.servlet.HandlerExceptionResolver;
* @since 3.1 * @since 3.1
*/ */
@Configuration @Configuration
class WebMvcConfiguration extends WebMvcConfigurationSupport { public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
@ -61,53 +54,52 @@ class WebMvcConfiguration extends WebMvcConfigurationSupport {
} }
@Override @Override
protected void configureInterceptors(InterceptorConfigurer configurer) { protected final void configureInterceptors(InterceptorConfigurer configurer) {
configurers.configureInterceptors(configurer); configurers.configureInterceptors(configurer);
} }
@Override @Override
protected void configureViewControllers(ViewControllerConfigurer configurer) { protected final void configureViewControllers(ViewControllerConfigurer configurer) {
configurers.configureViewControllers(configurer); configurers.configureViewControllers(configurer);
} }
@Override @Override
protected void configureResourceHandling(ResourceConfigurer configurer) { protected final void configureResourceHandling(ResourceConfigurer configurer) {
configurers.configureResourceHandling(configurer); configurers.configureResourceHandling(configurer);
} }
@Override @Override
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { protected final void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurers.configureDefaultServletHandling(configurer); configurers.configureDefaultServletHandling(configurer);
} }
@Override @Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { protected final void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
configurers.addArgumentResolvers(argumentResolvers); configurers.addArgumentResolvers(argumentResolvers);
} }
@Override @Override
protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) { protected final void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
configurers.addReturnValueHandlers(returnValueHandlers); configurers.addReturnValueHandlers(returnValueHandlers);
} }
@Override @Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { protected final void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
configurers.configureMessageConverters(converters); configurers.configureMessageConverters(converters);
} }
@Override @Override
protected void addFormatters(FormatterRegistry registry) { protected final void addFormatters(FormatterRegistry registry) {
configurers.addFormatters(registry); configurers.addFormatters(registry);
} }
@Override @Override
protected Validator getValidator() { protected final Validator getValidator() {
return configurers.getValidator(); return configurers.getValidator();
} }
@Override @Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { protected final void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
configurers.configureHandlerExceptionResolvers(exceptionResolvers); configurers.configureHandlerExceptionResolvers(exceptionResolvers);
} }

View File

@ -25,7 +25,7 @@ import org.springframework.web.servlet.DispatcherServlet;
/** /**
* Enables default Spring MVC configuration and registers Spring MVC infrastructure components expected by the * Enables default Spring MVC configuration and registers Spring MVC infrastructure components expected by the
* {@link DispatcherServlet}. Add this annotation to an application @{@link Configuration} class. It will in * {@link DispatcherServlet}. Add this annotation to an application @{@link Configuration} class. It will in
* turn import the @{@link Configuration} class {@link WebMvcConfiguration}, which provides default Spring MVC * turn import the @{@link Configuration} class {@link DelegatingWebMvcConfiguration}, which provides default Spring MVC
* configuration. * configuration.
* <pre class="code"> * <pre class="code">
* &#064;Configuration * &#064;Configuration
@ -76,6 +76,6 @@ import org.springframework.web.servlet.DispatcherServlet;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Documented @Documented
@Import(WebMvcConfiguration.class) @Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc { public @interface EnableWebMvc {
} }

View File

@ -265,7 +265,7 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
ConfigurableWebBindingInitializer webBindingInitializer = new ConfigurableWebBindingInitializer(); ConfigurableWebBindingInitializer webBindingInitializer = new ConfigurableWebBindingInitializer();
webBindingInitializer.setConversionService(mvcConversionService()); webBindingInitializer.setConversionService(mvcConversionService());
webBindingInitializer.setValidator(mvcValidator()); webBindingInitializer.setValidator(mvcValidator());
extendWebBindingInitializer(webBindingInitializer); configureWebBindingInitializer(webBindingInitializer);
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<HandlerMethodArgumentResolver>(); List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
addArgumentResolvers(argumentResolvers); addArgumentResolvers(argumentResolvers);
@ -285,7 +285,7 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
* Override this method to customize the {@link ConfigurableWebBindingInitializer} the * Override this method to customize the {@link ConfigurableWebBindingInitializer} the
* {@link RequestMappingHandlerAdapter} is configured with. * {@link RequestMappingHandlerAdapter} is configured with.
*/ */
protected void extendWebBindingInitializer(ConfigurableWebBindingInitializer webBindingInitializer) { protected void configureWebBindingInitializer(ConfigurableWebBindingInitializer webBindingInitializer) {
} }
/** /**

View File

@ -63,14 +63,14 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv
*/ */
public class WebMvcConfigurationTests { public class WebMvcConfigurationTests {
private WebMvcConfiguration mvcConfiguration; private DelegatingWebMvcConfiguration mvcConfiguration;
private WebMvcConfigurer configurer; private WebMvcConfigurer configurer;
@Before @Before
public void setUp() { public void setUp() {
configurer = EasyMock.createMock(WebMvcConfigurer.class); configurer = EasyMock.createMock(WebMvcConfigurer.class);
mvcConfiguration = new WebMvcConfiguration(); mvcConfiguration = new DelegatingWebMvcConfiguration();
} }
@Test @Test
@ -114,7 +114,7 @@ public class WebMvcConfigurationTests {
converters.add(new StringHttpMessageConverter()); converters.add(new StringHttpMessageConverter());
} }
}); });
mvcConfiguration = new WebMvcConfiguration(); mvcConfiguration = new DelegatingWebMvcConfiguration();
mvcConfiguration.setConfigurers(configurers); mvcConfiguration.setConfigurers(configurers);
adapter = mvcConfiguration.requestMappingHandlerAdapter(); adapter = mvcConfiguration.requestMappingHandlerAdapter();