Class SpringServletContainerInitializer
- java.lang.Object
- org.springframework.web.SpringServletContainerInitializer
- All Implemented Interfaces:
ServletContainerInitializer
public class SpringServletContainerInitializer extends Object implements ServletContainerInitializer
Servlet 3.0ServletContainerInitializer
designed to support code-based configuration of the servlet container using Spring'sWebApplicationInitializer
SPI as opposed to (or possibly in combination with) the traditionalweb.xml
-based approach.Mechanism of Operation
This class will be loaded and instantiated and have itsonStartup(java.util.Set<java.lang.Class<?>>, javax.servlet.ServletContext)
method invoked by any Servlet 3.0-compliant container during container startup assuming that thespring-web
module JAR is present on the classpath. This occurs through the JAR Services APIServiceLoader.load(Class)
method detecting thespring-web
module'sMETA-INF/services/javax.servlet.ServletContainerInitializer
service provider configuration file. See the JAR Services API documentation as well as section 8.2.4 of the Servlet 3.0 Final Draft specification for complete details.In combination with
A web application can choose to limit the amount of classpath scanning the Servlet container does at startup either through theweb.xml
metadata-complete
attribute inweb.xml
, which controls scanning for Servlet annotations or through an<absolute-ordering>
element also inweb.xml
, which controls which web fragments (i.e. jars) are allowed to perform aServletContainerInitializer
scan. When using this feature, theSpringServletContainerInitializer
can be enabled by adding "spring_web" to the list of named web fragments inweb.xml
as follows:<absolute-ordering> <name>some_web_fragment</name> <name>spring_web</name> </absolute-ordering>
Relationship to Spring's
Spring'sWebApplicationInitializer
WebApplicationInitializer
SPI consists of just one method:WebApplicationInitializer.onStartup(ServletContext)
. The signature is intentionally quite similar toServletContainerInitializer.onStartup(Set, ServletContext)
: simply put,SpringServletContainerInitializer
is responsible for instantiating and delegating theServletContext
to any user-definedWebApplicationInitializer
implementations. It is then the responsibility of eachWebApplicationInitializer
to do the actual work of initializing theServletContext
. The exact process of delegation is described in detail in theonStartup
documentation below.General Notes
In general, this class should be viewed as supporting infrastructure for the more important and user-facingWebApplicationInitializer
SPI. Taking advantage of this container initializer is also completely optional: while it is true that this initializer will be loaded and invoked under all Servlet 3.0+ runtimes, it remains the user's choice whether to make anyWebApplicationInitializer
implementations available on the classpath. If noWebApplicationInitializer
types are detected, this container initializer will have no effect.Note that use of this container initializer and of
WebApplicationInitializer
is not in any way "tied" to Spring MVC other than the fact that the types are shipped in thespring-web
module JAR. Rather, they can be considered general-purpose in their ability to facilitate convenient code-based configuration of theServletContext
. In other words, any servlet, listener, or filter may be registered within aWebApplicationInitializer
, not just Spring MVC-specific components.This class is neither designed for extension nor intended to be extended. It should be considered an internal type, with
WebApplicationInitializer
being the public-facing SPI.See Also
SeeWebApplicationInitializer
Javadoc for examples and detailed usage recommendations.- Since:
- 3.1
- Author:
- Chris Beams, Juergen Hoeller, Rossen Stoyanchev
- See Also:
onStartup(Set, ServletContext)
,WebApplicationInitializer
Constructor Summary
Constructors Constructor Description SpringServletContainerInitializer()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
Delegate theServletContext
to anyWebApplicationInitializer
implementations present on the application classpath.
Constructor Detail
SpringServletContainerInitializer
public SpringServletContainerInitializer()
Method Detail
onStartup
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException
Delegate theServletContext
to anyWebApplicationInitializer
implementations present on the application classpath.Because this class declares @
HandlesTypes(WebApplicationInitializer.class)
, Servlet 3.0+ containers will automatically scan the classpath for implementations of Spring'sWebApplicationInitializer
interface and provide the set of all such types to thewebAppInitializerClasses
parameter of this method.If no
WebApplicationInitializer
implementations are found on the classpath, this method is effectively a no-op. An INFO-level log message will be issued notifying the user that theServletContainerInitializer
has indeed been invoked but that noWebApplicationInitializer
implementations were found.Assuming that one or more
WebApplicationInitializer
types are detected, they will be instantiated (and sorted if the @@Order
annotation is present or theOrdered
interface has been implemented). Then theWebApplicationInitializer.onStartup(ServletContext)
method will be invoked on each instance, delegating theServletContext
such that each instance may register and configure servlets such as Spring'sDispatcherServlet
, listeners such as Spring'sContextLoaderListener
, or any other Servlet API componentry such as filters.- Specified by:
onStartup
in interfaceServletContainerInitializer
- Parameters:
webAppInitializerClasses
- all implementations ofWebApplicationInitializer
found on the application classpathservletContext
- the servlet context to be initialized- Throws:
ServletException
- if an error has occurred- See Also:
WebApplicationInitializer.onStartup(ServletContext)
,AnnotationAwareOrderComparator