写点什么

Spring 源码 -MVC 启动

用户头像
云淡风轻
关注
发布于: 2020 年 06 月 18 日

加载

Servlet context listener (web.xml) approach

  1. A web application WAR is being deployed by user.

  2. Servlet container (Tomcat) reads web.xml.

  3. Servlet context listener ContextLoaderListener is being instantiated (if defined as <listener> inside the web.xmlby servlet container.ContextLoaderListener creates new WebApplicationContext with application context XML configuration.Your ROOT context beans are registered and instantiated by BeanFactory inside the application context.

  4. DispatcherServlet is being instantiated by servlet container.DispatcherServlet creates its own WebApplicationContext (WEB-INF/{servletName}-servlet.xml by default) with the ROOT context as its parent.Your servlet beans are registered and instantiated by BeanFactory inside the application context.DispatcherServlet registers some default beans in case you did not provide them yourself.

<!--spring的上下文监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>



Servlet container initializer (non web.xml) approach

  1. A web application WAR is being deployed by user.

  2. Servlet container searches for classes implementing ServletContainerInitializer via Java's ServiceLoader.

  3. Spring's SpringServletContainerInitializer is found and instantiated by servlet container.

  4. Spring's initializer reads web application's class-path and searches for WebApplicationInitializer implementations.

  5. Your WebApplicationInitializer is found (btw. check its JavaDoc!!!) and instantiated by SpringServletContainerInitializer.Your WebApplicationInitializer creates new ROOT WebApplicationContext with XML or @Configuration based configuration.Your WebApplicationInitializer creates new servlet WebApplicationContext with XML or @Configuration based configuration.Your WebApplicationInitializer creates and registers new DispatcherServlet with the context from previous step.

  6. Servlet container finishes the web application initialization and instantiates components which were registered by their class in previous steps (none in my example).



Java based approach is much more flexible. You can leave the context creation to DispatcherServlet or even the whole instantiation of DispatcherServlet itself to servlet container (just register servlet DispatcherServlet.class instead of its instance).




源码解析

ContextLoader

Performs the actual initialization work for the root application context.Called by ContextLoaderListener. Looks for a contextClass parameter at the web.xml context-param level to specify the context class type, falling back to XmlWebApplicationContext if not found. With the default ContextLoader implementation, any context class specified needs to implement the ConfigurableWebApplicationContext interface.



ContextLoaderListener

Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener.



ApplicationContext

Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.



WebApplicationContext

Interface to provide configuration for a web application. This is read-only while the application is running, but may be reloaded if the implementation supports this.



XmlWebApplicationContext

XmlWebApplicationContext implementation which takes its configuration from XML documents, understood by an XmlBeanDefinitionReader. This is essentially the equivalent of GenericXmlApplicationContext for a web environment. By default, the configuration will be taken from "/WEB-INF/applicationContext.xml" for the root context.



ConfigurableWebApplicationContext

Interface to be implemented by configurable web application contexts.Supported by ContextLoader and FrameworkServlet.



java.util.EventListener

A tagging interface that all event listener interfaces must extend.



javax.servlet.ServletContextListener

Interface for receiving notification events about ServletContext lifecycle changes.



javax.servlet.ServletContext

Defines a set of methods that a servlet uses to communicate with its servlet container.

There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a *.war file.)



Web容器启动流程

当Servlet容器启动或终止Web 应用时,会触发ServletContextEvent事件,该事件由ServletContextListener来处理。ServletContextListener接口中定义了处理ServletContextEvent事件的两个方法。

Servlet API中有一个ServletContextListener接口,它能够监听ServletContext对象的生命周期,实际上就是监听 Web 应用的生命周期。

public interface ServletContextListener extends EventListener {
/**
* Receives notification that the web application initialization
* process is starting.
*
* <p>All ServletContextListeners are notified of context
* initialization before any filters or servlets in the web
* application are initialized.
*
* @param sce the ServletContextEvent containing the ServletContext
* that is being initialized
*
* @implSpec
* The default implementation takes no action.
*/
default public void contextInitialized(ServletContextEvent sce) {}
/**
* Receives notification that the ServletContext is about to be
* shut down.
*
* <p>All servlets and filters will have been destroyed before any
* ServletContextListeners are notified of context
* destruction.
*
* @param sce the ServletContextEvent containing the ServletContext
* that is being destroyed
*
* @implSpec
* The default implementation takes no action.
*/
default public void contextDestroyed(ServletContextEvent sce) {}
}



用户头像

云淡风轻

关注

云淡风轻 2018.08.18 加入

JAVA软件工程师

评论

发布
暂无评论
Spring源码-MVC启动