spring加载流程
1.监听器加载spring
2.加载配置文件
3.工厂生产实例化对象
4.放入ServletContext
springmvc加载流程
1.Servlet加载(监听器之后即执行)Servlet的init()
2.加载配置文件
3.从ServletContext拿到spring初始化springmvc相关对象
4.放入ServletContext
springmvc执行流程
1.用户请求到DispatcherServlet
2.DispatcherServlet查找HandlerMapping请求Handler并返回查找结果
3.DispatcherServlet调用HandlerAdapter执行Handler并返回执行结果
4.DispatcherServlet调用ResolverView生成视图并返回视图
5.DispatcherServlet返回给用户
WEB项目启动时候,加载web.xml,spring会在web.xml中配置启动监听器和启动参数
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>
-
classpath:spring-*.xml
-
</param-value>
-
</context-param>
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-
</listener>
web容器启动加载spring容器顺序
- 执行web.xml中的ContextLoaderListener监听器
- 初始化ContextLoaderListener中的contextInitialized方法
-
/**
-
* Initialize the root web application context.
-
*/
-
public void contextInitialized(ServletContextEvent event) {
-
this.contextLoader = createContextLoader();
-
if (this.contextLoader == null) {
-
this.contextLoader = this;
-
}
-
this.contextLoader.initWebApplicationContext(event.getServletContext());
-
}
调用父类的initWebApplicationContext
在initwebapplicationContext中调用了三个方法
创建WebApplicationContext对象
-
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
-
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
-
throw new IllegalStateException(
-
"Cannot initialize context because there is already a root application context present - " +
-
"check whether you have multiple ContextLoader* definitions in your web.xml!");
-
}
-
Log logger = LogFactory.getLog(ContextLoader.class);
-
servletContext.log("Initializing Spring root WebApplicationContext");
-
if (logger.isInfoEnabled()) {
-
logger.info("Root WebApplicationContext: initialization started");
-
}
-
long startTime = System.currentTimeMillis();
-
try {
-
// Store context in local instance variable, to guarantee that
-
// it is available on ServletContext shutdown.
-
if (this.context == null) {
-
this.context = createWebApplicationContext(servletContext);
-
}
-
if (this.context instanceof ConfigurableWebApplicationContext) {
-
configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);
-
}
-
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
-
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
-
if (ccl == ContextLoader.class.getClassLoader()) {
-
currentContext = this.context;
-
}
-
else if (ccl != null) {
-
currentContextPerThread.put(ccl, this.context);
-
}
-
if (logger.isDebugEnabled()) {
-
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
-
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
-
}
-
if (logger.isInfoEnabled()) {
-
long elapsedTime = System.currentTimeMillis() - startTime;
-
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
-
}
-
return this.context;
-
}
-
catch (RuntimeException ex) {
-
logger.error("Context initialization failed", ex);
-
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
-
throw ex;
-
}
-
catch (Error err) {
-
logger.error("Context initialization failed", err);
-
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
-
throw err;
-
}
-
}
加载context-param中配置的spring配置文件
初始化配置文件中及创建配置文件中的bean
-
/**
-
* Name of servlet context parameter (i.e., {@value}) that can specify the
-
* config location for the root context, falling back to the implementation's
-
* default otherwise.
-
* @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
-
*/
-
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
---------------------------------------------------------------------------------------------------------------------完美分割线
web容器停止时候,停止webapplicationContent容器
-
/**
-
* Close the root web application context.
-
*/
-
public void contextDestroyed(ServletContextEvent event) {
-
if (this.contextLoader != null) {
-
this.contextLoader.closeWebApplicationContext(event.getServletContext());
-
}
-
ContextCleanupListener.cleanupAttributes(event.getServletContext());
-
}