Skip to content

Commit 21eb8db

Browse files
committed
Polishing
Issue: SPR-11357
1 parent defc1d3 commit 21eb8db

File tree

4 files changed

+68
-69
lines changed

4 files changed

+68
-69
lines changed

spring-web/src/main/java/org/springframework/web/context/AbstractContextLoaderInitializer.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -25,25 +25,26 @@
2525
import org.springframework.web.WebApplicationInitializer;
2626

2727
/**
28-
* Convenient base class for {@link WebApplicationInitializer} implementations that
29-
* register a {@link ContextLoaderListener} in the servlet context.
28+
* Convenient base class for {@link WebApplicationInitializer} implementations
29+
* that register a {@link ContextLoaderListener} in the servlet context.
3030
*
31-
* <p>The only method required to be implemented by subclasses is {@link
32-
* #createRootApplicationContext()}, which gets invoked from {@link
33-
* #registerContextLoaderListener(javax.servlet.ServletContext)}.
31+
* <p>The only method required to be implemented by subclasses is
32+
* {@link #createRootApplicationContext()}, which gets invoked from
33+
* {@link #registerContextLoaderListener(ServletContext)}.
3434
*
3535
* @author Arjen Poutsma
3636
* @author Chris Beams
3737
* @since 3.2
3838
*/
3939
public abstract class AbstractContextLoaderInitializer implements WebApplicationInitializer {
4040

41-
/** Logger available to subclasses. */
41+
/** Logger available to subclasses */
4242
protected final Log logger = LogFactory.getLog(getClass());
4343

44+
4445
@Override
4546
public void onStartup(ServletContext servletContext) throws ServletException {
46-
this.registerContextLoaderListener(servletContext);
47+
registerContextLoaderListener(servletContext);
4748
}
4849

4950
/**
@@ -53,7 +54,7 @@ public void onStartup(ServletContext servletContext) throws ServletException {
5354
* @param servletContext the servlet context to register the listener against
5455
*/
5556
protected void registerContextLoaderListener(ServletContext servletContext) {
56-
WebApplicationContext rootAppContext = this.createRootApplicationContext();
57+
WebApplicationContext rootAppContext = createRootApplicationContext();
5758
if (rootAppContext != null) {
5859
servletContext.addListener(new ContextLoaderListener(rootAppContext));
5960
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@
1616

1717
package org.springframework.web.servlet.support;
1818

19-
import org.springframework.util.Assert;
2019
import org.springframework.util.ObjectUtils;
2120
import org.springframework.web.context.WebApplicationContext;
2221
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
2322

2423
/**
25-
* Base class for {@link org.springframework.web.WebApplicationInitializer
26-
* WebApplicationInitializer} implementations that register a {@link
27-
* org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
28-
* configured with annotated classes, e.g. Spring's {@link
29-
* org.springframework.context.annotation.Configuration @Configuration} classes.
24+
* Base class for {@link org.springframework.web.WebApplicationInitializer}
25+
* implementations that register a
26+
* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
27+
* configured with annotated classes, e.g. Spring's
28+
* {@link org.springframework.context.annotation.Configuration @Configuration} classes.
3029
*
3130
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()}
32-
* and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. Further
33-
* template and customization methods are provided by {@link
34-
* AbstractDispatcherServletInitializer}.
31+
* and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}.
32+
* Further template and customization methods are provided by
33+
* {@link AbstractDispatcherServletInitializer}.
3534
*
3635
* @author Arjen Poutsma
3736
* @author Chris Beams
@@ -48,10 +47,10 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
4847
*/
4948
@Override
5049
protected WebApplicationContext createRootApplicationContext() {
51-
Class<?>[] rootConfigClasses = this.getRootConfigClasses();
52-
if (!ObjectUtils.isEmpty(rootConfigClasses)) {
50+
Class<?>[] configClasses = getRootConfigClasses();
51+
if (!ObjectUtils.isEmpty(configClasses)) {
5352
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
54-
rootAppContext.register(rootConfigClasses);
53+
rootAppContext.register(configClasses);
5554
return rootAppContext;
5655
}
5756
else {
@@ -67,7 +66,7 @@ protected WebApplicationContext createRootApplicationContext() {
6766
@Override
6867
protected WebApplicationContext createServletApplicationContext() {
6968
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
70-
Class<?>[] configClasses = this.getServletConfigClasses();
69+
Class<?>[] configClasses = getServletConfigClasses();
7170
if (!ObjectUtils.isEmpty(configClasses)) {
7271
servletAppContext.register(configClasses);
7372
}
@@ -89,7 +88,7 @@ protected WebApplicationContext createServletApplicationContext() {
8988
* provided to the {@linkplain #createServletApplicationContext() dispatcher servlet
9089
* application context}.
9190
* @return the configuration classes for the dispatcher servlet application context
92-
* (may not be empty or {@code null}).
91+
* (may not be empty or {@code null})
9392
*/
9493
protected abstract Class<?>[] getServletConfigClasses();
9594

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.servlet.support;
1818

1919
import java.util.EnumSet;
20-
2120
import javax.servlet.DispatcherType;
2221
import javax.servlet.Filter;
2322
import javax.servlet.FilterRegistration;
@@ -34,14 +33,13 @@
3433
import org.springframework.web.servlet.DispatcherServlet;
3534

3635
/**
37-
* Base class for {@link org.springframework.web.WebApplicationInitializer
38-
* WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in
39-
* the servlet context.
36+
* Base class for {@link org.springframework.web.WebApplicationInitializer}
37+
* implementations that register a {@link DispatcherServlet} in the servlet context.
4038
*
41-
* <p>Concrete implementations are required to implement {@link
42-
* #createServletApplicationContext()}, as well as {@link #getServletMappings()}, both of
43-
* which get invoked from {@link #registerDispatcherServlet(ServletContext)}. Further
44-
* customization can be achieved by overriding
39+
* <p>Concrete implementations are required to implement
40+
* {@link #createServletApplicationContext()}, as well as {@link #getServletMappings()},
41+
* both of which get invoked from {@link #registerDispatcherServlet(ServletContext)}.
42+
* Further customization can be achieved by overriding
4543
* {@link #customizeRegistration(ServletRegistration.Dynamic)}.
4644
*
4745
* <p>Because this class extends from {@link AbstractContextLoaderInitializer}, concrete
@@ -55,24 +53,24 @@
5553
* @author Rossen Stoyanchev
5654
* @since 3.2
5755
*/
58-
public abstract class AbstractDispatcherServletInitializer
59-
extends AbstractContextLoaderInitializer {
56+
public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer {
6057

6158
/**
6259
* The default servlet name. Can be customized by overriding {@link #getServletName}.
6360
*/
6461
public static final String DEFAULT_SERVLET_NAME = "dispatcher";
6562

63+
6664
@Override
6765
public void onStartup(ServletContext servletContext) throws ServletException {
6866
super.onStartup(servletContext);
6967

70-
this.registerDispatcherServlet(servletContext);
68+
registerDispatcherServlet(servletContext);
7169
}
7270

7371
/**
7472
* Register a {@link DispatcherServlet} against the given servlet context.
75-
* <p>This method will create a {@code DispatcherServlet} with the name returned from
73+
* <p>This method will create a {@code DispatcherServlet} with the name returned by
7674
* {@link #getServletName()}, initializing it with the application context returned
7775
* from {@link #createServletApplicationContext()}, and mapping it to the patterns
7876
* returned from {@link #getServletMappings()}.
@@ -81,21 +79,18 @@ public void onStartup(ServletContext servletContext) throws ServletException {
8179
* @param servletContext the context to register the servlet against
8280
*/
8381
protected void registerDispatcherServlet(ServletContext servletContext) {
84-
String servletName = this.getServletName();
82+
String servletName = getServletName();
8583
Assert.hasLength(servletName, "getServletName() may not return empty or null");
8684

87-
WebApplicationContext servletAppContext = this.createServletApplicationContext();
85+
WebApplicationContext servletAppContext = createServletApplicationContext();
8886
Assert.notNull(servletAppContext,
8987
"createServletApplicationContext() did not return an application " +
90-
"context for servlet [" + servletName + "]");
88+
"context for servlet [" + servletName + "]");
9189

9290
DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
93-
94-
ServletRegistration.Dynamic registration =
95-
servletContext.addServlet(servletName, dispatcherServlet);
96-
91+
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
9792
Assert.notNull(registration,
98-
"Failed to register servlet with name '" + servletName + "'. " +
93+
"Failed to register servlet with name '" + servletName + "'." +
9994
"Check if there is another servlet registered under the same name.");
10095

10196
registration.setLoadOnStartup(1);
@@ -109,7 +104,7 @@ protected void registerDispatcherServlet(ServletContext servletContext) {
109104
}
110105
}
111106

112-
this.customizeRegistration(registration);
107+
customizeRegistration(registration);
113108
}
114109

115110
/**
@@ -124,8 +119,8 @@ protected String getServletName() {
124119
/**
125120
* Create a servlet application context to be provided to the {@code DispatcherServlet}.
126121
* <p>The returned context is delegated to Spring's
127-
* {@link DispatcherServlet#DispatcherServlet(WebApplicationContext)}. As such, it
128-
* typically contains controllers, view resolvers, locale resolvers, and other
122+
* {@link DispatcherServlet#DispatcherServlet(WebApplicationContext)}. As such,
123+
* it typically contains controllers, view resolvers, locale resolvers, and other
129124
* web-related beans.
130125
* @see #registerDispatcherServlet(ServletContext)
131126
*/
@@ -140,7 +135,6 @@ protected String getServletName() {
140135

141136
/**
142137
* Specify filters to add and map to the {@code DispatcherServlet}.
143-
*
144138
* @return an array of filters or {@code null}
145139
* @see #registerServletFilter(ServletContext, Filter)
146140
*/
@@ -161,7 +155,6 @@ protected Filter[] getServletFilters() {
161155
* </ul>
162156
* <p>If the above defaults are not suitable or insufficient, override this
163157
* method and register filters directly with the {@code ServletContext}.
164-
*
165158
* @param servletContext the servlet context to register filters with
166159
* @param filter the filter to be registered
167160
* @return the filter registration

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@
1616

1717
package org.springframework.web.servlet.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertTrue;
23-
24-
import java.util.*;
25-
26-
import javax.servlet.*;
19+
import java.util.Collections;
20+
import java.util.EnumSet;
21+
import java.util.EventListener;
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
import javax.servlet.DispatcherType;
25+
import javax.servlet.Filter;
2726
import javax.servlet.FilterRegistration.Dynamic;
27+
import javax.servlet.Servlet;
28+
import javax.servlet.ServletContextEvent;
29+
import javax.servlet.ServletContextListener;
30+
import javax.servlet.ServletException;
31+
import javax.servlet.ServletRegistration;
2832

2933
import org.junit.Before;
3034
import org.junit.Test;
35+
3136
import org.springframework.context.annotation.Bean;
3237
import org.springframework.context.annotation.Configuration;
3338
import org.springframework.mock.web.test.MockServletConfig;
@@ -37,6 +42,8 @@
3742
import org.springframework.web.filter.HiddenHttpMethodFilter;
3843
import org.springframework.web.servlet.DispatcherServlet;
3944

45+
import static org.junit.Assert.*;
46+
4047
/**
4148
* Test case for {@link AbstractAnnotationConfigDispatcherServletInitializer}.
4249
*
@@ -130,13 +137,12 @@ protected boolean isAsyncSupported() {
130137
}
131138

132139
// SPR-11357
133-
134140
@Test
135141
public void rootContextOnly() throws ServletException {
136142
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
137143
@Override
138144
protected Class<?>[] getRootConfigClasses() {
139-
return new Class[]{MyConfiguration.class};
145+
return new Class<?>[] {MyConfiguration.class};
140146
}
141147
@Override
142148
protected Class<?>[] getServletConfigClasses() {
@@ -173,6 +179,13 @@ protected Filter[] getServletFilters() {
173179

174180
private class MyMockServletContext extends MockServletContext {
175181

182+
@Override
183+
public <T extends EventListener> void addListener(T t) {
184+
if (t instanceof ServletContextListener) {
185+
((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
186+
}
187+
}
188+
176189
@Override
177190
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
178191
servlets.put(servletName, servlet);
@@ -188,15 +201,9 @@ public Dynamic addFilter(String filterName, Filter filter) {
188201
filterRegistrations.put(filterName, registration);
189202
return registration;
190203
}
191-
192-
@Override
193-
public <T extends EventListener> void addListener(T t) {
194-
if (t instanceof ServletContextListener) {
195-
((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
196-
}
197-
}
198204
}
199205

206+
200207
private static class MyAnnotationConfigDispatcherServletInitializer
201208
extends AbstractAnnotationConfigDispatcherServletInitializer {
202209

@@ -229,13 +236,13 @@ protected void customizeRegistration(ServletRegistration.Dynamic registration) {
229236
protected Class<?>[] getRootConfigClasses() {
230237
return null;
231238
}
232-
233239
}
234240

235-
private static class MyBean {
236241

242+
private static class MyBean {
237243
}
238244

245+
239246
@Configuration
240247
@SuppressWarnings("unused")
241248
private static class MyConfiguration {
@@ -247,7 +254,6 @@ public MyConfiguration() {
247254
public MyBean bean() {
248255
return new MyBean();
249256
}
250-
251257
}
252258

253259
}

0 commit comments

Comments
 (0)