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 c95b8a1bd4d..005aa136084 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 @@ -629,6 +629,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv */ protected final void addDefaultHandlerExceptionResolvers(List exceptionResolvers) { ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver = new ExceptionHandlerExceptionResolver(); + exceptionHandlerExceptionResolver.setApplicationContext(this.applicationContext); exceptionHandlerExceptionResolver.setContentNegotiationManager(mvcContentNegotiationManager()); exceptionHandlerExceptionResolver.setMessageConverters(getMessageConverters()); exceptionHandlerExceptionResolver.afterPropertiesSet(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java index 3c5ad6c7739..aac015e80ed 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java @@ -44,8 +44,11 @@ import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor; import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite; +import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver; +import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; /** * A test fixture with an {@link WebMvcConfigurationSupport} instance. @@ -56,20 +59,24 @@ public class WebMvcConfigurationSupportTests { private WebMvcConfigurationSupport mvcConfiguration; + private StaticWebApplicationContext wac; + + @Before public void setUp() { - mvcConfiguration = new WebMvcConfigurationSupport(); + this.wac = new StaticWebApplicationContext(); + this.mvcConfiguration = new WebMvcConfigurationSupport(); + this.mvcConfiguration.setApplicationContext(wac); } @Test public void requestMappingHandlerMapping() throws Exception { - StaticWebApplicationContext cxt = new StaticWebApplicationContext(); - cxt.registerSingleton("controller", TestController.class); + this.wac.registerSingleton("controller", TestController.class); RequestMappingHandlerMapping handlerMapping = mvcConfiguration.requestMappingHandlerMapping(); assertEquals(0, handlerMapping.getOrder()); - handlerMapping.setApplicationContext(cxt); + handlerMapping.setApplicationContext(this.wac); handlerMapping.afterPropertiesSet(); HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/")); assertNotNull(chain.getInterceptors()); @@ -146,9 +153,14 @@ public class WebMvcConfigurationSupportTests { assertEquals(0, compositeResolver.getOrder()); - List expectedResolvers = new ArrayList(); - mvcConfiguration.addDefaultHandlerExceptionResolvers(expectedResolvers); - assertEquals(expectedResolvers.size(), compositeResolver.getExceptionResolvers().size()); + List expectedResolvers = compositeResolver.getExceptionResolvers(); + + assertEquals(ExceptionHandlerExceptionResolver.class, expectedResolvers.get(0).getClass()); + assertEquals(ResponseStatusExceptionResolver.class, expectedResolvers.get(1).getClass()); + assertEquals(DefaultHandlerExceptionResolver.class, expectedResolvers.get(2).getClass()); + + ExceptionHandlerExceptionResolver eher = (ExceptionHandlerExceptionResolver) expectedResolvers.get(0); + assertNotNull(eher.getApplicationContext()); }