Skip to content

Commit d738ddd

Browse files
committed
Add createDispatcherServlet hook point
Add an extra hook point in `AbstractDispatcherServletInitializer` to customize the `DispatcherServlet`. Issue: SPR-13222
1 parent 84138ab commit d738ddd

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractDispatcherServletInitializer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -53,6 +53,7 @@
5353
* @author Chris Beams
5454
* @author Rossen Stoyanchev
5555
* @author Juergen Hoeller
56+
* @author Stephane Nicoll
5657
* @since 3.2
5758
*/
5859
public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer {
@@ -76,7 +77,8 @@ public void onStartup(ServletContext servletContext) throws ServletException {
7677
* from {@link #createServletApplicationContext()}, and mapping it to the patterns
7778
* returned from {@link #getServletMappings()}.
7879
* <p>Further customization can be achieved by overriding {@link
79-
* #customizeRegistration(ServletRegistration.Dynamic)}.
80+
* #customizeRegistration(ServletRegistration.Dynamic)} or
81+
* {@link #createDispatcherServlet(WebApplicationContext)}.
8082
* @param servletContext the context to register the servlet against
8183
*/
8284
protected void registerDispatcherServlet(ServletContext servletContext) {
@@ -88,7 +90,7 @@ protected void registerDispatcherServlet(ServletContext servletContext) {
8890
"createServletApplicationContext() did not return an application " +
8991
"context for servlet [" + servletName + "]");
9092

91-
DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
93+
DispatcherServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
9294
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
9395

9496
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
@@ -129,6 +131,13 @@ protected String getServletName() {
129131
*/
130132
protected abstract WebApplicationContext createServletApplicationContext();
131133

134+
/**
135+
* Create a {@link DispatcherServlet} with the specified {@link WebApplicationContext}.
136+
*/
137+
protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
138+
return new DispatcherServlet(servletAppContext);
139+
}
140+
132141
/**
133142
* Specify application context initializers to be applied to the servlet-specific
134143
* application context that the {@code DispatcherServlet} is being created with.

spring-webmvc/src/test/java/org/springframework/web/servlet/support/DispatcherServletInitializerTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -70,6 +70,7 @@ public void register() throws ServletException {
7070
assertNotNull(servlets.get(SERVLET_NAME));
7171

7272
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
73+
assertEquals(MyDispatcherServlet.class, servlet.getClass());
7374
WebApplicationContext servletContext = servlet.getWebApplicationContext();
7475

7576
assertTrue(servletContext.containsBean("bean"));
@@ -84,6 +85,7 @@ public void register() throws ServletException {
8485
assertEquals(ROLE_NAME, registration.getRunAsRole());
8586
}
8687

88+
8789
private class MyMockServletContext extends MockServletContext {
8890

8991
@Override
@@ -104,6 +106,11 @@ protected String getServletName() {
104106
return SERVLET_NAME;
105107
}
106108

109+
@Override
110+
protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
111+
return new MyDispatcherServlet(servletAppContext);
112+
}
113+
107114
@Override
108115
protected WebApplicationContext createServletApplicationContext() {
109116
StaticWebApplicationContext servletContext =
@@ -129,7 +136,12 @@ protected WebApplicationContext createRootApplicationContext() {
129136
}
130137

131138
private static class MyBean {
139+
}
132140

141+
private static class MyDispatcherServlet extends DispatcherServlet {
142+
public MyDispatcherServlet(WebApplicationContext webApplicationContext) {
143+
super(webApplicationContext);
144+
}
133145
}
134146

135147
}

src/asciidoc/web-mvc.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ based equivalent of the above `web.xml` example:
205205

206206
`WebApplicationInitializer` is an interface provided by Spring MVC that ensures your
207207
code-based configuration is detected and automatically used to initialize any Servlet 3
208-
container. An abstract base class implementation of this interace named
208+
container. An abstract base class implementation of this interface named
209209
`AbstractDispatcherServletInitializer` makes it even easier to register the
210210
`DispatcherServlet` by simply specifying its servlet mapping.
211211
See <<mvc-container-config,Code-based Servlet container initialization>> for more details.
@@ -4462,6 +4462,9 @@ The `isAsyncSupported` protected method of `AbstractDispatcherServletInitializer
44624462
provides a single place to enable async support on the `DispatcherServlet` and all
44634463
filters mapped to it. By default this flag is set to `true`.
44644464

4465+
Finally, if you need to further customize the `DispatcherServlet` itself, you can
4466+
override the `createDispatcherServlet` method.
4467+
44654468

44664469

44674470

src/asciidoc/whats-new.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ public @interface MyTestConfig {
525525
* When using GSON or Jackson 2.6+, the handler method return type is used to improve
526526
serialization of parameterized types like `List<Foo>`.
527527
* Default JSON prefix has been changed from "{} && " to the safer ")]}', " one.
528+
* Protected `createDispatcherServlet` method in `AbstractDispatcherServletInitializer` to further
529+
customize the `DispatcherServlet` instance to use.
528530

529531
=== WebSocket Messaging Improvements
530532

0 commit comments

Comments
 (0)