diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java index 084acd90c6f..2c181c594b4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.util.CollectionUtils; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; import org.springframework.web.method.support.HandlerMethodArgumentResolver; @@ -29,7 +30,7 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.HandlerExceptionResolver; /** - * A sub-class of {@code WebMvcConfigurationSupport} that detects and delegates + * A subclass of {@code WebMvcConfigurationSupport} that detects and delegates * to all beans of type {@link WebMvcConfigurer} allowing them to customize the * configuration provided by {@code WebMvcConfigurationSupport}. This is the * class actually imported by {@link EnableWebMvc @EnableWebMvc}. @@ -45,16 +46,15 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { @Autowired(required = false) public void setConfigurers(List configurers) { - if (configurers == null || configurers.isEmpty()) { - return; + if (!CollectionUtils.isEmpty(configurers)) { + this.configurers.addWebMvcConfigurers(configurers); } - this.configurers.addWebMvcConfigurers(configurers); } @Override - protected void addInterceptors(InterceptorRegistry registry) { - this.configurers.addInterceptors(registry); + protected void configurePathMatch(PathMatchConfigurer configurer) { + this.configurers.configurePathMatch(configurer); } @Override @@ -63,23 +63,23 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { } @Override - public void configureAsyncSupport(AsyncSupportConfigurer configurer) { + protected void configureAsyncSupport(AsyncSupportConfigurer configurer) { this.configurers.configureAsyncSupport(configurer); } @Override - public void configurePathMatch(PathMatchConfigurer configurer) { - this.configurers.configurePathMatch(configurer); + protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + this.configurers.configureDefaultServletHandling(configurer); } @Override - protected void addViewControllers(ViewControllerRegistry registry) { - this.configurers.addViewControllers(registry); + protected void addFormatters(FormatterRegistry registry) { + this.configurers.addFormatters(registry); } @Override - protected void configureViewResolvers(ViewResolverRegistry registry) { - this.configurers.configureViewResolvers(registry); + protected void addInterceptors(InterceptorRegistry registry) { + this.configurers.addInterceptors(registry); } @Override @@ -88,8 +88,18 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { } @Override - protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { - this.configurers.configureDefaultServletHandling(configurer); + protected void addCorsMappings(CorsRegistry registry) { + this.configurers.addCorsMappings(registry); + } + + @Override + protected void addViewControllers(ViewControllerRegistry registry) { + this.configurers.addViewControllers(registry); + } + + @Override + protected void configureViewResolvers(ViewResolverRegistry registry) { + this.configurers.configureViewResolvers(registry); } @Override @@ -113,8 +123,13 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { } @Override - protected void addFormatters(FormatterRegistry registry) { - this.configurers.addFormatters(registry); + protected void configureHandlerExceptionResolvers(List exceptionResolvers) { + this.configurers.configureHandlerExceptionResolvers(exceptionResolvers); + } + + @Override + protected void extendHandlerExceptionResolvers(List exceptionResolvers) { + this.configurers.extendHandlerExceptionResolvers(exceptionResolvers); } @Override @@ -127,14 +142,4 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { return this.configurers.getMessageCodesResolver(); } - @Override - protected void configureHandlerExceptionResolvers(List exceptionResolvers) { - this.configurers.configureHandlerExceptionResolvers(exceptionResolvers); - } - - @Override - protected void addCorsMappings(CorsRegistry registry) { - this.configurers.addCorsMappings(registry); - } - } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 3a114972154..6d5ab799626 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -315,7 +315,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv * @see PathMatchConfigurer * @since 4.0.3 */ - public void configurePathMatch(PathMatchConfigurer configurer) { + protected void configurePathMatch(PathMatchConfigurer configurer) { } /** @@ -531,6 +531,13 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv return initializer; } + /** + * Override this method to configure asynchronous request processing options. + * @see AsyncSupportConfigurer + */ + protected void configureAsyncSupport(AsyncSupportConfigurer configurer) { + } + /** * Return a {@link FormattingConversionService} for use with annotated * controller methods and the {@code spring:eval} JSP tag. @@ -543,6 +550,12 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv return conversionService; } + /** + * Override this method to add custom {@link Converter}s and {@link Formatter}s. + */ + protected void addFormatters(FormatterRegistry registry) { + } + /** * Return a global {@link Validator} instance for example for validating * {@code @ModelAttribute} and {@code @RequestBody} method arguments. @@ -762,19 +775,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv } } - /** - * Override this method to add custom {@link Converter}s and {@link Formatter}s. - */ - protected void addFormatters(FormatterRegistry registry) { - } - - /** - * Override this method to configure asynchronous request processing options. - * @see AsyncSupportConfigurer - */ - public void configureAsyncSupport(AsyncSupportConfigurer configurer) { - } - /** * Return an instance of {@link CompositeUriComponentsContributor} for use with * {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}. diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java index 6ab3eb410bb..ca00aec989d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java @@ -46,51 +46,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl */ public interface WebMvcConfigurer { - /** - * Add {@link Converter}s and {@link Formatter}s in addition to the ones - * registered by default. - */ - void addFormatters(FormatterRegistry registry); - - /** - * Configure the {@link HttpMessageConverter}s to use for reading or writing - * to the body of the request or response. If no converters are added, a - * default list of converters is registered. - *

Note that adding converters to the list, turns off - * default converter registration. To simply add a converter without impacting - * default registration, consider using the method - * {@link #extendMessageConverters(java.util.List)} instead. - * @param converters initially an empty list of converters - */ - void configureMessageConverters(List> converters); - - /** - * A hook for extending or modifying the list of converters after it has been - * configured. This may be useful for example to allow default converters to - * be registered and then insert a custom converter through this method. - * @param converters the list of configured converters to extend. - * @since 4.1.3 - */ - void extendMessageConverters(List> converters); - - /** - * Provide a custom {@link Validator} instead of the one created by default. - * The default implementation, assuming JSR-303 is on the classpath, is: - * {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}. - * Leave the return value as {@code null} to keep the default. - */ - Validator getValidator(); - - /** - * Configure content negotiation options. - */ - void configureContentNegotiation(ContentNegotiationConfigurer configurer); - - /** - * Configure asynchronous request handling options. - */ - void configureAsyncSupport(AsyncSupportConfigurer configurer); - /** * Helps with configuring HandlerMappings path matching options such as trailing slash match, * suffix registration, path matcher and path helper. @@ -105,40 +60,28 @@ public interface WebMvcConfigurer { void configurePathMatch(PathMatchConfigurer configurer); /** - * Add resolvers to support custom controller method argument types. - *

This does not override the built-in support for resolving handler - * method arguments. To customize the built-in support for argument - * resolution, configure {@link RequestMappingHandlerAdapter} directly. - * @param argumentResolvers initially an empty list + * Configure content negotiation options. */ - void addArgumentResolvers(List argumentResolvers); + void configureContentNegotiation(ContentNegotiationConfigurer configurer); /** - * Add handlers to support custom controller method return value types. - *

Using this option does not override the built-in support for handling - * return values. To customize the built-in support for handling return - * values, configure RequestMappingHandlerAdapter directly. - * @param returnValueHandlers initially an empty list + * Configure asynchronous request handling options. */ - void addReturnValueHandlers(List returnValueHandlers); + void configureAsyncSupport(AsyncSupportConfigurer configurer); /** - * Configure the {@link HandlerExceptionResolver}s to handle unresolved - * controller exceptions. If no resolvers are added to the list, default - * exception resolvers are added instead. - * @param exceptionResolvers initially an empty list + * Configure a handler to delegate unhandled requests by forwarding to the + * Servlet container's "default" servlet. A common use case for this is when + * the {@link DispatcherServlet} is mapped to "/" thus overriding the + * Servlet container's default handling of static resources. */ - void configureHandlerExceptionResolvers(List exceptionResolvers); + void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); /** - * A hook for extending or modifying the list of - * {@link HandlerExceptionResolver}s after it has been configured. This may - * be useful for example to allow default resolvers to be registered and then - * insert a custom one through this method. - * @param exceptionResolvers the list of configured resolvers to extend. - * @since 4.3 + * Add {@link Converter}s and {@link Formatter}s in addition to the ones + * registered by default. */ - void extendHandlerExceptionResolvers(List exceptionResolvers); + void addFormatters(FormatterRegistry registry); /** * Add Spring MVC lifecycle interceptors for pre- and post-processing of @@ -155,11 +98,17 @@ public interface WebMvcConfigurer { void addInterceptors(InterceptorRegistry registry); /** - * Provide a custom {@link MessageCodesResolver} for building message codes - * from data binding and validation error codes. Leave the return value as - * {@code null} to keep the default. + * Add handlers to serve static resources such as images, js, and, css + * files from specific locations under web application root, the classpath, + * and others. */ - MessageCodesResolver getMessageCodesResolver(); + void addResourceHandlers(ResourceHandlerRegistry registry); + + /** + * Configure cross origin requests processing. + * @since 4.2 + */ + void addCorsMappings(CorsRegistry registry); /** * Configure simple automated controllers pre-configured with the response @@ -178,24 +127,74 @@ public interface WebMvcConfigurer { void configureViewResolvers(ViewResolverRegistry registry); /** - * Add handlers to serve static resources such as images, js, and, css - * files from specific locations under web application root, the classpath, - * and others. + * Add resolvers to support custom controller method argument types. + *

This does not override the built-in support for resolving handler + * method arguments. To customize the built-in support for argument + * resolution, configure {@link RequestMappingHandlerAdapter} directly. + * @param argumentResolvers initially an empty list */ - void addResourceHandlers(ResourceHandlerRegistry registry); + void addArgumentResolvers(List argumentResolvers); /** - * Configure a handler to delegate unhandled requests by forwarding to the - * Servlet container's "default" servlet. A common use case for this is when - * the {@link DispatcherServlet} is mapped to "/" thus overriding the - * Servlet container's default handling of static resources. + * Add handlers to support custom controller method return value types. + *

Using this option does not override the built-in support for handling + * return values. To customize the built-in support for handling return + * values, configure RequestMappingHandlerAdapter directly. + * @param returnValueHandlers initially an empty list */ - void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); + void addReturnValueHandlers(List returnValueHandlers); /** - * Configure cross origin requests processing. - * @since 4.2 + * Configure the {@link HttpMessageConverter}s to use for reading or writing + * to the body of the request or response. If no converters are added, a + * default list of converters is registered. + *

Note that adding converters to the list, turns off + * default converter registration. To simply add a converter without impacting + * default registration, consider using the method + * {@link #extendMessageConverters(java.util.List)} instead. + * @param converters initially an empty list of converters */ - void addCorsMappings(CorsRegistry registry); + void configureMessageConverters(List> converters); + + /** + * A hook for extending or modifying the list of converters after it has been + * configured. This may be useful for example to allow default converters to + * be registered and then insert a custom converter through this method. + * @param converters the list of configured converters to extend. + * @since 4.1.3 + */ + void extendMessageConverters(List> converters); + + /** + * Configure the {@link HandlerExceptionResolver}s to handle unresolved + * controller exceptions. If no resolvers are added to the list, default + * exception resolvers are added instead. + * @param exceptionResolvers initially an empty list + */ + void configureHandlerExceptionResolvers(List exceptionResolvers); + + /** + * A hook for extending or modifying the list of {@link HandlerExceptionResolver}s + * after it has been configured. This may be useful for example to allow default + * resolvers to be registered and then insert a custom one through this method. + * @param exceptionResolvers the list of configured resolvers to extend + * @since 4.3 + */ + void extendHandlerExceptionResolvers(List exceptionResolvers); + + /** + * Provide a custom {@link Validator} instead of the one created by default. + * The default implementation, assuming JSR-303 is on the classpath, is: + * {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}. + * Leave the return value as {@code null} to keep the default. + */ + Validator getValidator(); + + /** + * Provide a custom {@link MessageCodesResolver} for building message codes + * from data binding and validation error codes. Leave the return value as + * {@code null} to keep the default. + */ + MessageCodesResolver getMessageCodesResolver(); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java index 1c9e3e0c7e5..3d1c28845e3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ import org.springframework.web.servlet.HandlerExceptionResolver; /** * An implementation of {@link WebMvcConfigurer} with empty methods allowing - * sub-classes to override only the methods they're interested in. + * subclasses to override only the methods they're interested in. * * @author Rossen Stoyanchev * @since 3.1 @@ -40,7 +40,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void addFormatters(FormatterRegistry registry) { + public void configurePathMatch(PathMatchConfigurer configurer) { } /** @@ -48,7 +48,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configureMessageConverters(List> converters) { + public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { } /** @@ -56,16 +56,15 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void extendMessageConverters(List> converters) { + public void configureAsyncSupport(AsyncSupportConfigurer configurer) { } /** * {@inheritDoc} - *

This implementation returns {@code null} + *

This implementation is empty. */ @Override - public Validator getValidator() { - return null; + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { } /** @@ -73,7 +72,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { + public void addFormatters(FormatterRegistry registry) { } /** @@ -81,7 +80,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configureAsyncSupport(AsyncSupportConfigurer configurer) { + public void addInterceptors(InterceptorRegistry registry) { } /** @@ -89,7 +88,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configurePathMatch(PathMatchConfigurer configurer) { + public void addResourceHandlers(ResourceHandlerRegistry registry) { } /** @@ -97,7 +96,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void addArgumentResolvers(List argumentResolvers) { + public void addCorsMappings(CorsRegistry registry) { } /** @@ -105,7 +104,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void addReturnValueHandlers(List returnValueHandlers) { + public void addViewControllers(ViewControllerRegistry registry) { } /** @@ -113,7 +112,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configureHandlerExceptionResolvers(List exceptionResolvers) { + public void configureViewResolvers(ViewResolverRegistry registry) { } /** @@ -121,7 +120,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void extendHandlerExceptionResolvers(List exceptionResolvers) { + public void addArgumentResolvers(List argumentResolvers) { } /** @@ -129,8 +128,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public MessageCodesResolver getMessageCodesResolver() { - return null; + public void addReturnValueHandlers(List returnValueHandlers) { } /** @@ -138,7 +136,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void addInterceptors(InterceptorRegistry registry) { + public void configureMessageConverters(List> converters) { } /** @@ -146,7 +144,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void addViewControllers(ViewControllerRegistry registry) { + public void extendMessageConverters(List> converters) { } /** @@ -154,7 +152,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configureViewResolvers(ViewResolverRegistry registry) { + public void configureHandlerExceptionResolvers(List exceptionResolvers) { } /** @@ -162,23 +160,25 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { + public void extendHandlerExceptionResolvers(List exceptionResolvers) { } /** * {@inheritDoc} - *

This implementation is empty. + *

This implementation returns {@code null}. */ @Override - public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + public Validator getValidator() { + return null; } /** * {@inheritDoc} - *

This implementation is empty. + *

This implementation returns {@code null}. */ @Override - public void addCorsMappings(CorsRegistry registry) { + public MessageCodesResolver getMessageCodesResolver() { + return null; } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java index 1b6d6e53cb7..c2b62ee2d3c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java @@ -21,6 +21,7 @@ import java.util.List; import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.util.CollectionUtils; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; import org.springframework.web.method.support.HandlerMethodArgumentResolver; @@ -37,16 +38,18 @@ class WebMvcConfigurerComposite implements WebMvcConfigurer { private final List delegates = new ArrayList(); + public void addWebMvcConfigurers(List configurers) { - if (configurers != null) { + if (!CollectionUtils.isEmpty(configurers)) { this.delegates.addAll(configurers); } } + @Override - public void addFormatters(FormatterRegistry registry) { + public void configurePathMatch(PathMatchConfigurer configurer) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.addFormatters(registry); + delegate.configurePathMatch(configurer); } } @@ -65,131 +68,126 @@ class WebMvcConfigurerComposite implements WebMvcConfigurer { } @Override - public void configurePathMatch(PathMatchConfigurer configurer) { + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configurePathMatch(configurer); + delegate.configureDefaultServletHandling(configurer); } } @Override - public void configureMessageConverters(List> converters) { + public void addFormatters(FormatterRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configureMessageConverters(converters); + delegate.addFormatters(registry); } } @Override - public void extendMessageConverters(List> converters) { + public void addInterceptors(InterceptorRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.extendMessageConverters(converters); + delegate.addInterceptors(registry); } } @Override - public void addArgumentResolvers(List argumentResolvers) { + public void addResourceHandlers(ResourceHandlerRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.addArgumentResolvers(argumentResolvers); + delegate.addResourceHandlers(registry); } } @Override - public void addReturnValueHandlers(List returnValueHandlers) { + public void addCorsMappings(CorsRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.addReturnValueHandlers(returnValueHandlers); + delegate.addCorsMappings(registry); } } @Override - public void configureHandlerExceptionResolvers(List exceptionResolvers) { + public void addViewControllers(ViewControllerRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configureHandlerExceptionResolvers(exceptionResolvers); + delegate.addViewControllers(registry); } } @Override - public void extendHandlerExceptionResolvers(List exceptionResolvers) { + public void configureViewResolvers(ViewResolverRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configureHandlerExceptionResolvers(exceptionResolvers); + delegate.configureViewResolvers(registry); } } @Override - public void addInterceptors(InterceptorRegistry registry) { + public void addArgumentResolvers(List argumentResolvers) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.addInterceptors(registry); + delegate.addArgumentResolvers(argumentResolvers); } } @Override - public void addViewControllers(ViewControllerRegistry registry) { + public void addReturnValueHandlers(List returnValueHandlers) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.addViewControllers(registry); + delegate.addReturnValueHandlers(returnValueHandlers); } } @Override - public void configureViewResolvers(ViewResolverRegistry registry) { + public void configureMessageConverters(List> converters) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configureViewResolvers(registry); + delegate.configureMessageConverters(converters); } } @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { + public void extendMessageConverters(List> converters) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.addResourceHandlers(registry); + delegate.extendMessageConverters(converters); } } @Override - public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + public void configureHandlerExceptionResolvers(List exceptionResolvers) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configureDefaultServletHandling(configurer); + delegate.configureHandlerExceptionResolvers(exceptionResolvers); + } + } + + @Override + public void extendHandlerExceptionResolvers(List exceptionResolvers) { + for (WebMvcConfigurer delegate : this.delegates) { + delegate.extendHandlerExceptionResolvers(exceptionResolvers); } } @Override public Validator getValidator() { - List candidates = new ArrayList(); + Validator selected = null; for (WebMvcConfigurer configurer : this.delegates) { Validator validator = configurer.getValidator(); if (validator != null) { - candidates.add(validator); + if (selected != null) { + throw new IllegalStateException("No unique Validator found: {" + + selected + ", " + validator + "}"); + } + selected = validator; } } - return selectSingleInstance(candidates, Validator.class); - } - - @Override - public void addCorsMappings(CorsRegistry registry) { - for (WebMvcConfigurer delegate : this.delegates) { - delegate.addCorsMappings(registry); - } - } - - private T selectSingleInstance(List instances, Class instanceType) { - if (instances.size() > 1) { - throw new IllegalStateException( - "Only one [" + instanceType + "] was expected but multiple instances were provided: " + instances); - } - else if (instances.size() == 1) { - return instances.get(0); - } - else { - return null; - } + return selected; } @Override public MessageCodesResolver getMessageCodesResolver() { - List candidates = new ArrayList(); + MessageCodesResolver selected = null; for (WebMvcConfigurer configurer : this.delegates) { MessageCodesResolver messageCodesResolver = configurer.getMessageCodesResolver(); if (messageCodesResolver != null) { - candidates.add(messageCodesResolver); + if (selected != null) { + throw new IllegalStateException("No unique MessageCodesResolver found: {" + + selected + ", " + messageCodesResolver + "}"); + } + selected = messageCodesResolver; } } - return selectSingleInstance(candidates, MessageCodesResolver.class); + return selected; } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java index 258b67a9a22..d0c58268453 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.springframework.web.servlet.config.annotation; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.junit.Before; @@ -89,10 +89,10 @@ public class DelegatingWebMvcConfigurationTests { delegatingConfig = new DelegatingWebMvcConfiguration(); } + @Test public void requestMappingHandlerAdapter() throws Exception { - - delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer)); + delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer)); RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter(); ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer(); @@ -141,7 +141,7 @@ public class DelegatingWebMvcConfigurationTests { public void getCustomValidator() { given(webMvcConfigurer.getValidator()).willReturn(new LocalValidatorFactoryBean()); - delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer)); + delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer)); delegatingConfig.mvcValidator(); verify(webMvcConfigurer).getValidator(); @@ -151,7 +151,7 @@ public class DelegatingWebMvcConfigurationTests { public void getCustomMessageCodesResolver() { given(webMvcConfigurer.getMessageCodesResolver()).willReturn(new DefaultMessageCodesResolver()); - delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer)); + delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer)); delegatingConfig.getMessageCodesResolver(); verify(webMvcConfigurer).getMessageCodesResolver(); @@ -159,8 +159,7 @@ public class DelegatingWebMvcConfigurationTests { @Test public void handlerExceptionResolver() throws Exception { - - delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer)); + delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer)); delegatingConfig.handlerExceptionResolver(); verify(webMvcConfigurer).configureMessageConverters(converters.capture()); @@ -186,7 +185,7 @@ public class DelegatingWebMvcConfigurationTests { delegatingConfig.setConfigurers(configurers); HandlerExceptionResolverComposite composite = - (HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver(); + (HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver(); assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size()); } @@ -200,9 +199,9 @@ public class DelegatingWebMvcConfigurationTests { @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseRegisteredSuffixPatternMatch(true) - .setUseTrailingSlashMatch(false) - .setUrlPathHelper(pathHelper) - .setPathMatcher(pathMatcher); + .setUseTrailingSlashMatch(false) + .setUrlPathHelper(pathHelper) + .setPathMatcher(pathMatcher); } }); delegatingConfig.setConfigurers(configurers);