diff --git a/spring-web/src/main/java/org/springframework/web/context/AbstractContextLoaderInitializer.java b/spring-web/src/main/java/org/springframework/web/context/AbstractContextLoaderInitializer.java index 636aceacfbe..ecbf5a91da6 100644 --- a/spring-web/src/main/java/org/springframework/web/context/AbstractContextLoaderInitializer.java +++ b/spring-web/src/main/java/org/springframework/web/context/AbstractContextLoaderInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -25,12 +25,12 @@ import org.apache.commons.logging.LogFactory; import org.springframework.web.WebApplicationInitializer; /** - * Convenient base class for {@link WebApplicationInitializer} implementations that - * register a {@link ContextLoaderListener} in the servlet context. + * Convenient base class for {@link WebApplicationInitializer} implementations + * that register a {@link ContextLoaderListener} in the servlet context. * - *

The only method required to be implemented by subclasses is {@link - * #createRootApplicationContext()}, which gets invoked from {@link - * #registerContextLoaderListener(javax.servlet.ServletContext)}. + *

The only method required to be implemented by subclasses is + * {@link #createRootApplicationContext()}, which gets invoked from + * {@link #registerContextLoaderListener(ServletContext)}. * * @author Arjen Poutsma * @author Chris Beams @@ -38,12 +38,13 @@ import org.springframework.web.WebApplicationInitializer; */ public abstract class AbstractContextLoaderInitializer implements WebApplicationInitializer { - /** Logger available to subclasses. */ + /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); + @Override public void onStartup(ServletContext servletContext) throws ServletException { - this.registerContextLoaderListener(servletContext); + registerContextLoaderListener(servletContext); } /** @@ -53,7 +54,7 @@ public abstract class AbstractContextLoaderInitializer implements WebApplication * @param servletContext the servlet context to register the listener against */ protected void registerContextLoaderListener(ServletContext servletContext) { - WebApplicationContext rootAppContext = this.createRootApplicationContext(); + WebApplicationContext rootAppContext = createRootApplicationContext(); if (rootAppContext != null) { servletContext.addListener(new ContextLoaderListener(rootAppContext)); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.java index d71d6c076e2..6a1777fe10f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.java @@ -16,22 +16,21 @@ package org.springframework.web.servlet.support; -import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; /** - * Base class for {@link org.springframework.web.WebApplicationInitializer - * WebApplicationInitializer} implementations that register a {@link - * org.springframework.web.servlet.DispatcherServlet DispatcherServlet} - * configured with annotated classes, e.g. Spring's {@link - * org.springframework.context.annotation.Configuration @Configuration} classes. + * Base class for {@link org.springframework.web.WebApplicationInitializer} + * implementations that register a + * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} + * configured with annotated classes, e.g. Spring's + * {@link org.springframework.context.annotation.Configuration @Configuration} classes. * *

Concrete implementations are required to implement {@link #getRootConfigClasses()} - * and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. Further - * template and customization methods are provided by {@link - * AbstractDispatcherServletInitializer}. + * and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. + * Further template and customization methods are provided by + * {@link AbstractDispatcherServletInitializer}. * * @author Arjen Poutsma * @author Chris Beams @@ -48,10 +47,10 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer */ @Override protected WebApplicationContext createRootApplicationContext() { - Class[] rootConfigClasses = this.getRootConfigClasses(); - if (!ObjectUtils.isEmpty(rootConfigClasses)) { + Class[] configClasses = getRootConfigClasses(); + if (!ObjectUtils.isEmpty(configClasses)) { AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); - rootAppContext.register(rootConfigClasses); + rootAppContext.register(configClasses); return rootAppContext; } else { @@ -67,7 +66,7 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext(); - Class[] configClasses = this.getServletConfigClasses(); + Class[] configClasses = getServletConfigClasses(); if (!ObjectUtils.isEmpty(configClasses)) { servletAppContext.register(configClasses); } @@ -89,7 +88,7 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer * provided to the {@linkplain #createServletApplicationContext() dispatcher servlet * application context}. * @return the configuration classes for the dispatcher servlet application context - * (may not be empty or {@code null}). + * (may not be empty or {@code null}) */ protected abstract Class[] getServletConfigClasses(); 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 e2c58d3fa2c..4b978700054 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -17,7 +17,6 @@ package org.springframework.web.servlet.support; import java.util.EnumSet; - import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterRegistration; @@ -34,14 +33,13 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; /** - * Base class for {@link org.springframework.web.WebApplicationInitializer - * WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in - * the servlet context. + * Base class for {@link org.springframework.web.WebApplicationInitializer} + * implementations that register a {@link DispatcherServlet} in the servlet context. * - *

Concrete implementations are required to implement {@link - * #createServletApplicationContext()}, as well as {@link #getServletMappings()}, both of - * which get invoked from {@link #registerDispatcherServlet(ServletContext)}. Further - * customization can be achieved by overriding + *

Concrete implementations are required to implement + * {@link #createServletApplicationContext()}, as well as {@link #getServletMappings()}, + * both of which get invoked from {@link #registerDispatcherServlet(ServletContext)}. + * Further customization can be achieved by overriding * {@link #customizeRegistration(ServletRegistration.Dynamic)}. * *

Because this class extends from {@link AbstractContextLoaderInitializer}, concrete @@ -55,24 +53,24 @@ import org.springframework.web.servlet.DispatcherServlet; * @author Rossen Stoyanchev * @since 3.2 */ -public abstract class AbstractDispatcherServletInitializer - extends AbstractContextLoaderInitializer { +public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer { /** * The default servlet name. Can be customized by overriding {@link #getServletName}. */ public static final String DEFAULT_SERVLET_NAME = "dispatcher"; + @Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); - this.registerDispatcherServlet(servletContext); + registerDispatcherServlet(servletContext); } /** * Register a {@link DispatcherServlet} against the given servlet context. - *

This method will create a {@code DispatcherServlet} with the name returned from + *

This method will create a {@code DispatcherServlet} with the name returned by * {@link #getServletName()}, initializing it with the application context returned * from {@link #createServletApplicationContext()}, and mapping it to the patterns * returned from {@link #getServletMappings()}. @@ -81,21 +79,18 @@ public abstract class AbstractDispatcherServletInitializer * @param servletContext the context to register the servlet against */ protected void registerDispatcherServlet(ServletContext servletContext) { - String servletName = this.getServletName(); + String servletName = getServletName(); Assert.hasLength(servletName, "getServletName() may not return empty or null"); - WebApplicationContext servletAppContext = this.createServletApplicationContext(); + WebApplicationContext servletAppContext = createServletApplicationContext(); Assert.notNull(servletAppContext, "createServletApplicationContext() did not return an application " + - "context for servlet [" + servletName + "]"); + "context for servlet [" + servletName + "]"); DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext); - - ServletRegistration.Dynamic registration = - servletContext.addServlet(servletName, dispatcherServlet); - + ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet); Assert.notNull(registration, - "Failed to register servlet with name '" + servletName + "'. " + + "Failed to register servlet with name '" + servletName + "'." + "Check if there is another servlet registered under the same name."); registration.setLoadOnStartup(1); @@ -109,7 +104,7 @@ public abstract class AbstractDispatcherServletInitializer } } - this.customizeRegistration(registration); + customizeRegistration(registration); } /** @@ -124,8 +119,8 @@ public abstract class AbstractDispatcherServletInitializer /** * Create a servlet application context to be provided to the {@code DispatcherServlet}. *

The returned context is delegated to Spring's - * {@link DispatcherServlet#DispatcherServlet(WebApplicationContext)}. As such, it - * typically contains controllers, view resolvers, locale resolvers, and other + * {@link DispatcherServlet#DispatcherServlet(WebApplicationContext)}. As such, + * it typically contains controllers, view resolvers, locale resolvers, and other * web-related beans. * @see #registerDispatcherServlet(ServletContext) */ @@ -140,7 +135,6 @@ public abstract class AbstractDispatcherServletInitializer /** * Specify filters to add and map to the {@code DispatcherServlet}. - * * @return an array of filters or {@code null} * @see #registerServletFilter(ServletContext, Filter) */ @@ -161,7 +155,6 @@ public abstract class AbstractDispatcherServletInitializer * *

If the above defaults are not suitable or insufficient, override this * method and register filters directly with the {@code ServletContext}. - * * @param servletContext the servlet context to register filters with * @param filter the filter to be registered * @return the filter registration diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/AnnotationConfigDispatcherServletInitializerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/AnnotationConfigDispatcherServletInitializerTests.java index a560e48535b..6e023649fa5 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/AnnotationConfigDispatcherServletInitializerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/AnnotationConfigDispatcherServletInitializerTests.java @@ -16,18 +16,23 @@ package org.springframework.web.servlet.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.*; - -import javax.servlet.*; +import java.util.Collections; +import java.util.EnumSet; +import java.util.EventListener; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.servlet.DispatcherType; +import javax.servlet.Filter; import javax.servlet.FilterRegistration.Dynamic; +import javax.servlet.Servlet; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; import org.junit.Before; import org.junit.Test; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mock.web.test.MockServletConfig; @@ -37,6 +42,8 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.servlet.DispatcherServlet; +import static org.junit.Assert.*; + /** * Test case for {@link AbstractAnnotationConfigDispatcherServletInitializer}. * @@ -130,13 +137,12 @@ public class AnnotationConfigDispatcherServletInitializerTests { } // SPR-11357 - @Test public void rootContextOnly() throws ServletException { initializer = new MyAnnotationConfigDispatcherServletInitializer() { @Override protected Class[] getRootConfigClasses() { - return new Class[]{MyConfiguration.class}; + return new Class[] {MyConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { @@ -173,6 +179,13 @@ public class AnnotationConfigDispatcherServletInitializerTests { private class MyMockServletContext extends MockServletContext { + @Override + public void addListener(T t) { + if (t instanceof ServletContextListener) { + ((ServletContextListener) t).contextInitialized(new ServletContextEvent(this)); + } + } + @Override public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) { servlets.put(servletName, servlet); @@ -188,15 +201,9 @@ public class AnnotationConfigDispatcherServletInitializerTests { filterRegistrations.put(filterName, registration); return registration; } - - @Override - public void addListener(T t) { - if (t instanceof ServletContextListener) { - ((ServletContextListener) t).contextInitialized(new ServletContextEvent(this)); - } - } } + private static class MyAnnotationConfigDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @@ -229,13 +236,13 @@ public class AnnotationConfigDispatcherServletInitializerTests { protected Class[] getRootConfigClasses() { return null; } - } + private static class MyBean { - } + @Configuration @SuppressWarnings("unused") private static class MyConfiguration { @@ -247,7 +254,6 @@ public class AnnotationConfigDispatcherServletInitializerTests { public MyBean bean() { return new MyBean(); } - } }