Spring MVC Servlet 3.0

1、Servlet 3.0 動態註冊資源(ServletContainerInitializer)

該類的API文檔:http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContainerInitializer.htmlhtml

Servlet 3.0 規範,能夠經過實現ServletContainerInitializer接口,在容器啓動階段,爲容器動態註冊Servlet、Filter 以及listeners。實現該接口的類必須在JAR文件的META-INF/services/javax.servlet.ServletContainerInitializer文件中,聲明全類名。java

在Spring-web.jar中,有以下目錄:web

$ find .
.
./META-INF
./META-INF/services
./META-INF/services/javax.servlet.ServletContainerInitializer
./org

查看內容以下:spring

$ cat ./META-INF/services/javax.servlet.ServletContainerInitializer
org.springframework.web.SpringServletContainerInitializer

能夠看到,Spring Web經過SpringServletContainerInitializer進行spring servlet初始化。對於SpringServletContainerInitializer,Spring API文檔說明以下:api

Servlet 3.0 ServletContainerInitializer designed to support code-based configuration of the servlet container using Spring's WebApplicationInitializer SPI as opposed to (or possibly in combination with) the traditional web.xml-based approach.bash

Mechanism of Operation

This class will be loaded and instantiated and have its onStartup method invoked by any Servlet 3.0-compliant container during container startup assuming that the spring-web module JAR is present on the classpath. This occurs through the JAR Services API ServiceLoader.load(Class) method detecting the spring-web module's META-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.
when in combination with web.xml

If a web application does include a WEB-INF/web.xml file, it is important to understand that neither this nor any other ServletContextInitializer will be processed unless the <web-app> element's version attribute is >= "3.0" and the xsi:schemaLocation for "http://java.sun.com/xml/ns/javaee" is set to "http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd".oracle

Relationship to Spring's WebApplicationInitializer

Spring's WebApplicationInitializer SPI consists of just one method: WebApplicationInitializer.onStartup(ServletContext). The signature is intentionally quite similar to ServletContainerInitializer.onStartup(Set, ServletContext): simply put, SpringServletContainerInitializer is responsible for instantiating and delegating the ServletContext to any user-defined WebApplicationInitializer implementations. It is then the responsibility of each WebApplicationInitializer to do the actual work of initializing the ServletContext. The exact process of delegation is described in detail in the onStartup documentation below.

General Notes

In general, this class should be viewed as supporting infrastructure for the more important and user-facing WebApplicationInitializer 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 any WebApplicationInitializer implementations available on the classpath. If no WebApplicationInitializer 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 the spring-web module JAR. Rather, they can be considered general-purpose in their ability to facilitate convenient code-based configuration of the ServletContext. Said another way, any servlet, listener, or filter may be registered within aWebApplicationInitializer, not just Spring MVC-specific components.app

This class is not designed for nor intended to be extended. It should be considered an internal type, with WebApplicationInitializer being the public-facing SPI.less

See Also

See WebApplicationInitializer Javadoc for examples and detailed usage recommendations.ide

 

AbstractDispatcherServletInitializer類實現了WebApplicationInitializer接口,用於註冊DispatcherServlet類。

相關文章
相關標籤/搜索