类 SpringServletContainerInitializer
- java.lang.Object
- org.springframework.web.SpringServletContainerInitializer
- 所有已实现的接口:
ServletContainerInitializer
public class SpringServletContainerInitializer extends Object implements ServletContainerInitializer
Servlet 3.0ServletContainerInitializerdesigned to support code-based configuration of the servlet container using Spring'sWebApplicationInitializerSPI 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-webmodule JAR is present on the classpath. This occurs through the JAR Services APIServiceLoader.load(Class)method detecting thespring-webmodule'sMETA-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.In combination with
A web application can choose to limit the amount of classpath scanning the Servlet container does at startup either through theweb.xmlmetadata-completeattribute 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 aServletContainerInitializerscan. When using this feature, theSpringServletContainerInitializercan be enabled by adding "spring_web" to the list of named web fragments inweb.xmlas follows:<absolute-ordering> <name>some_web_fragment</name> <name>spring_web</name> </absolute-ordering>
Relationship to Spring's
Spring'sWebApplicationInitializerWebApplicationInitializerSPI consists of just one method:WebApplicationInitializer.onStartup(ServletContext). The signature is intentionally quite similar toServletContainerInitializer.onStartup(Set, ServletContext): simply put,SpringServletContainerInitializeris responsible for instantiating and delegating theServletContextto any user-definedWebApplicationInitializerimplementations. It is then the responsibility of eachWebApplicationInitializerto do the actual work of initializing theServletContext. The exact process of delegation is described in detail in theonStartupdocumentation below.General Notes
In general, this class should be viewed as supporting infrastructure for the more important and user-facingWebApplicationInitializerSPI. 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 anyWebApplicationInitializerimplementations available on the classpath. If noWebApplicationInitializertypes are detected, this container initializer will have no effect.Note that use of this container initializer and of
WebApplicationInitializeris not in any way "tied" to Spring MVC other than the fact that the types are shipped in thespring-webmodule 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
WebApplicationInitializerbeing the public-facing SPI.See Also
SeeWebApplicationInitializerJavadoc for examples and detailed usage recommendations.- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams, Juergen Hoeller, Rossen Stoyanchev
- 另请参阅:
onStartup(Set, ServletContext),WebApplicationInitializer
构造器概要
构造器 构造器 说明 SpringServletContainerInitializer()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidonStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)Delegate theServletContextto anyWebApplicationInitializerimplementations present on the application classpath.
构造器详细资料
SpringServletContainerInitializer
public SpringServletContainerInitializer()
方法详细资料
onStartup
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException
Delegate theServletContextto anyWebApplicationInitializerimplementations 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'sWebApplicationInitializerinterface and provide the set of all such types to thewebAppInitializerClassesparameter of this method.If no
WebApplicationInitializerimplementations are found on the classpath, this method is effectively a no-op. An INFO-level log message will be issued notifying the user that theServletContainerInitializerhas indeed been invoked but that noWebApplicationInitializerimplementations were found.Assuming that one or more
WebApplicationInitializertypes are detected, they will be instantiated (and sorted if the @@Orderannotation is present or theOrderedinterface has been implemented). Then theWebApplicationInitializer.onStartup(ServletContext)method will be invoked on each instance, delegating theServletContextsuch 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.- 指定者:
onStartup在接口中ServletContainerInitializer- 参数:
webAppInitializerClasses- all implementations ofWebApplicationInitializerfound on the application classpathservletContext- the servlet context to be initialized- 抛出:
ServletException- if an error has occurred- 另请参阅:
WebApplicationInitializer.onStartup(ServletContext),AnnotationAwareOrderComparator