public class SpringServletContainerInitializer extends Object implements ServletContainerInitializer
ServletContainerInitializerdesigned to support code-based configuration of the servlet container using Spring's
WebApplicationInitializerSPI as opposed to (or possibly in combination with) the traditional
onStartup(java.util.Set<java.lang.Class<?>>, javax.servlet.ServletContext)method invoked by any Servlet 3.0-compliant container during container startup assuming that the
spring-webmodule JAR is present on the classpath. This occurs through the JAR Services API
ServiceLoader.load(Class)method detecting the
META-INF/services/javax.servlet.ServletContainerInitializerservice 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.
web.xml, which controls scanning for Servlet annotations or through an
<absolute-ordering>element also in
web.xml, which controls which web fragments (i.e. jars) are allowed to perform a
ServletContainerInitializerscan. When using this feature, the
SpringServletContainerInitializercan be enabled by adding "spring_web" to the list of named web fragments in
<absolute-ordering> <name>some_web_fragment</name> <name>spring_web</name> </absolute-ordering>
WebApplicationInitializerSPI consists of just one method:
WebApplicationInitializer.onStartup(ServletContext). The signature is intentionally quite similar to
ServletContainerInitializer.onStartup(Set, ServletContext): simply put,
SpringServletContainerInitializeris responsible for instantiating and delegating the
ServletContextto any user-defined
WebApplicationInitializerimplementations. It is then the responsibility of each
WebApplicationInitializerto do the actual work of initializing the
ServletContext. The exact process of delegation is described in detail in the
WebApplicationInitializerSPI. 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 any
WebApplicationInitializerimplementations available on the classpath. If no
WebApplicationInitializertypes are detected, this container initializer will have no effect.
Note that use of this container initializer and of
is not in any way "tied" to Spring MVC other than the fact that the types are shipped
spring-web module JAR. Rather, they can be considered general-purpose
in their ability to facilitate convenient code-based configuration of the
ServletContext. In other words, any servlet, listener, or filter may be
registered within a
WebApplicationInitializer, not just Spring MVC-specific
This class is neither designed for extension nor intended to be extended.
It should be considered an internal type, with
being the public-facing SPI.
WebApplicationInitializerJavadoc for examples and detailed usage recommendations.
|Constructor and Description|
|Modifier and Type||Method and Description|
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException
WebApplicationInitializerimplementations present on the application classpath.
Because this class declares @
Servlet 3.0+ containers will automatically scan the classpath for implementations
WebApplicationInitializer interface and provide the set of all
such types to the
webAppInitializerClasses parameter of this method.
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 the
ServletContainerInitializer has indeed been invoked but that
WebApplicationInitializer 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
Ordered interface has been
implemented). Then the
method will be invoked on each instance, delegating the
that each instance may register and configure servlets such as Spring's
DispatcherServlet, listeners such as Spring's
or any other Servlet API componentry such as filters.
webAppInitializerClasses- all implementations of
WebApplicationInitializerfound on the application classpath
servletContext- the servlet context to be initialized
ServletException- if an error has occurred