diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java index 6f47082894..6a29c1a8e5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -53,6 +53,7 @@ import org.springframework.web.servlet.DispatcherServlet; * @author Chris Beams * @author Rossen Stoyanchev * @author Juergen Hoeller + * @author Stephane Nicoll * @since 3.2 */ public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer { @@ -76,7 +77,8 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte * from {@link #createServletApplicationContext()}, and mapping it to the patterns * returned from {@link #getServletMappings()}. *

Further customization can be achieved by overriding {@link - * #customizeRegistration(ServletRegistration.Dynamic)}. + * #customizeRegistration(ServletRegistration.Dynamic)} or + * {@link #createDispatcherServlet(WebApplicationContext)}. * @param servletContext the context to register the servlet against */ protected void registerDispatcherServlet(ServletContext servletContext) { @@ -88,7 +90,7 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte "createServletApplicationContext() did not return an application " + "context for servlet [" + servletName + "]"); - DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext); + DispatcherServlet dispatcherServlet = createDispatcherServlet(servletAppContext); dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers()); ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet); @@ -129,6 +131,13 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte */ protected abstract WebApplicationContext createServletApplicationContext(); + /** + * Create a {@link DispatcherServlet} with the specified {@link WebApplicationContext}. + */ + protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) { + return new DispatcherServlet(servletAppContext); + } + /** * Specify application context initializers to be applied to the servlet-specific * application context that the {@code DispatcherServlet} is being created with. diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/DispatcherServletInitializerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/DispatcherServletInitializerTests.java index 568c8b619b..8eea0ea39d 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/DispatcherServletInitializerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/DispatcherServletInitializerTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 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. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -70,6 +70,7 @@ public class DispatcherServletInitializerTests { assertNotNull(servlets.get(SERVLET_NAME)); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); + assertEquals(MyDispatcherServlet.class, servlet.getClass()); WebApplicationContext servletContext = servlet.getWebApplicationContext(); assertTrue(servletContext.containsBean("bean")); @@ -84,6 +85,7 @@ public class DispatcherServletInitializerTests { assertEquals(ROLE_NAME, registration.getRunAsRole()); } + private class MyMockServletContext extends MockServletContext { @Override @@ -104,6 +106,11 @@ public class DispatcherServletInitializerTests { return SERVLET_NAME; } + @Override + protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) { + return new MyDispatcherServlet(servletAppContext); + } + @Override protected WebApplicationContext createServletApplicationContext() { StaticWebApplicationContext servletContext = @@ -129,7 +136,12 @@ public class DispatcherServletInitializerTests { } private static class MyBean { + } + private static class MyDispatcherServlet extends DispatcherServlet { + public MyDispatcherServlet(WebApplicationContext webApplicationContext) { + super(webApplicationContext); + } } } diff --git a/src/asciidoc/web-mvc.adoc b/src/asciidoc/web-mvc.adoc index 41b7d133a4..c3a207ad04 100644 --- a/src/asciidoc/web-mvc.adoc +++ b/src/asciidoc/web-mvc.adoc @@ -205,7 +205,7 @@ based equivalent of the above `web.xml` example: `WebApplicationInitializer` is an interface provided by Spring MVC that ensures your code-based configuration is detected and automatically used to initialize any Servlet 3 -container. An abstract base class implementation of this interace named +container. An abstract base class implementation of this interface named `AbstractDispatcherServletInitializer` makes it even easier to register the `DispatcherServlet` by simply specifying its servlet mapping. See <> for more details. @@ -4462,6 +4462,9 @@ The `isAsyncSupported` protected method of `AbstractDispatcherServletInitializer provides a single place to enable async support on the `DispatcherServlet` and all filters mapped to it. By default this flag is set to `true`. +Finally, if you need to further customize the `DispatcherServlet` itself, you can +override the `createDispatcherServlet` method. + diff --git a/src/asciidoc/whats-new.adoc b/src/asciidoc/whats-new.adoc index 4805c6f2fa..f879f03314 100644 --- a/src/asciidoc/whats-new.adoc +++ b/src/asciidoc/whats-new.adoc @@ -525,6 +525,8 @@ public @interface MyTestConfig { * When using GSON or Jackson 2.6+, the handler method return type is used to improve serialization of parameterized types like `List`. * Default JSON prefix has been changed from "{} && " to the safer ")]}', " one. +* Protected `createDispatcherServlet` method in `AbstractDispatcherServletInitializer` to further + customize the `DispatcherServlet` instance to use. === WebSocket Messaging Improvements