Browse Source

DelegatingWebMvcConfiguration properly delegates extendHandlerExceptionResolvers

Issue: SPR-14599
pull/25025/head
Juergen Hoeller 10 years ago
parent
commit
d2e3a1a4f5
  1. 15
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java
  2. 9
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java
  3. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java
  4. 21
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfigurationTests.java

15
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@ -29,7 +30,7 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver; 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 * to all beans of type {@link WebMvcConfigurer} allowing them to customize the
* configuration provided by {@code WebMvcConfigurationSupport}. This is the * configuration provided by {@code WebMvcConfigurationSupport}. This is the
* class actually imported by {@link EnableWebMvc @EnableWebMvc}. * class actually imported by {@link EnableWebMvc @EnableWebMvc}.
@ -45,10 +46,9 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
@Autowired(required = false) @Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) { public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (configurers == null || configurers.isEmpty()) { if (!CollectionUtils.isEmpty(configurers)) {
return; this.configurers.addWebMvcConfigurers(configurers);
} }
this.configurers.addWebMvcConfigurers(configurers);
} }
@ -132,6 +132,11 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
this.configurers.configureHandlerExceptionResolvers(exceptionResolvers); this.configurers.configureHandlerExceptionResolvers(exceptionResolvers);
} }
@Override
protected void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
this.configurers.extendHandlerExceptionResolvers(exceptionResolvers);
}
@Override @Override
protected void addCorsMappings(CorsRegistry registry) { protected void addCorsMappings(CorsRegistry registry) {
this.configurers.addCorsMappings(registry); this.configurers.addCorsMappings(registry);

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

@ -131,11 +131,10 @@ public interface WebMvcConfigurer {
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers); void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
/** /**
* A hook for extending or modifying the list of * A hook for extending or modifying the list of {@link HandlerExceptionResolver}s
* {@link HandlerExceptionResolver}s after it has been configured. This may * after it has been configured. This may be useful for example to allow default
* be useful for example to allow default resolvers to be registered and then * resolvers to be registered and then insert a custom one through this method.
* insert a custom one through this method. * @param exceptionResolvers the list of configured resolvers to extend
* @param exceptionResolvers the list of configured resolvers to extend.
* @since 4.3 * @since 4.3
*/ */
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers); void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);

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

@ -109,7 +109,7 @@ class WebMvcConfigurerComposite implements WebMvcConfigurer {
@Override @Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
for (WebMvcConfigurer delegate : this.delegates) { for (WebMvcConfigurer delegate : this.delegates) {
delegate.configureHandlerExceptionResolvers(exceptionResolvers); delegate.extendHandlerExceptionResolvers(exceptionResolvers);
} }
} }

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

@ -17,7 +17,7 @@
package org.springframework.web.servlet.config.annotation; package org.springframework.web.servlet.config.annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
@ -89,10 +89,10 @@ public class DelegatingWebMvcConfigurationTests {
delegatingConfig = new DelegatingWebMvcConfiguration(); delegatingConfig = new DelegatingWebMvcConfiguration();
} }
@Test @Test
public void requestMappingHandlerAdapter() throws Exception { public void requestMappingHandlerAdapter() throws Exception {
delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter(); RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer(); ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
@ -141,7 +141,7 @@ public class DelegatingWebMvcConfigurationTests {
public void getCustomValidator() { public void getCustomValidator() {
given(webMvcConfigurer.getValidator()).willReturn(new LocalValidatorFactoryBean()); given(webMvcConfigurer.getValidator()).willReturn(new LocalValidatorFactoryBean());
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer)); delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
delegatingConfig.mvcValidator(); delegatingConfig.mvcValidator();
verify(webMvcConfigurer).getValidator(); verify(webMvcConfigurer).getValidator();
@ -151,7 +151,7 @@ public class DelegatingWebMvcConfigurationTests {
public void getCustomMessageCodesResolver() { public void getCustomMessageCodesResolver() {
given(webMvcConfigurer.getMessageCodesResolver()).willReturn(new DefaultMessageCodesResolver()); given(webMvcConfigurer.getMessageCodesResolver()).willReturn(new DefaultMessageCodesResolver());
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer)); delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
delegatingConfig.getMessageCodesResolver(); delegatingConfig.getMessageCodesResolver();
verify(webMvcConfigurer).getMessageCodesResolver(); verify(webMvcConfigurer).getMessageCodesResolver();
@ -159,8 +159,7 @@ public class DelegatingWebMvcConfigurationTests {
@Test @Test
public void handlerExceptionResolver() throws Exception { public void handlerExceptionResolver() throws Exception {
delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
delegatingConfig.handlerExceptionResolver(); delegatingConfig.handlerExceptionResolver();
verify(webMvcConfigurer).configureMessageConverters(converters.capture()); verify(webMvcConfigurer).configureMessageConverters(converters.capture());
@ -186,7 +185,7 @@ public class DelegatingWebMvcConfigurationTests {
delegatingConfig.setConfigurers(configurers); delegatingConfig.setConfigurers(configurers);
HandlerExceptionResolverComposite composite = HandlerExceptionResolverComposite composite =
(HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver(); (HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver();
assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size()); assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size());
} }
@ -200,9 +199,9 @@ public class DelegatingWebMvcConfigurationTests {
@Override @Override
public void configurePathMatch(PathMatchConfigurer configurer) { public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseRegisteredSuffixPatternMatch(true) configurer.setUseRegisteredSuffixPatternMatch(true)
.setUseTrailingSlashMatch(false) .setUseTrailingSlashMatch(false)
.setUrlPathHelper(pathHelper) .setUrlPathHelper(pathHelper)
.setPathMatcher(pathMatcher); .setPathMatcher(pathMatcher);
} }
}); });
delegatingConfig.setConfigurers(configurers); delegatingConfig.setConfigurers(configurers);

Loading…
Cancel
Save