首先搞清楚,Spring的啟動過程說的其實也就是Spring容器的啟動過程,這是一回事。
spring的啟動是建築在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和監聽器(Listener),下面就來看看web.xml里面的配置:
接下來就一點的來解析這樣一個啟動過程。
從spring的上下文監聽器開始:
1、通過上述的第一段配置
<context-param>
是初始化上下文,然后通過后一段的<listener>來加載配置文件,其中調用的spring包中的
ContextLoaderListener
這個上下文監聽器,
ContextLoaderListener
是一個實現了
ServletContextListener
接口的監聽器,他的父類是
ContextLoader
,在啟動項目時會觸發
contextInitialized
上下文初始化方法。下面我們來看看這個方法:
public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
可以看到,這里是調用了父類
ContextLoader
的
initWebApplicationContext(event.getServletContext());
方法,很顯然,這是對ApplicationContext的初始化方法,也就是到這里 進入了springIoC的初始化。
2、接下來再來看看
initWebApplicationContext
又做了什么工作,先看看代碼
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) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, 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; }
這個方法還是有點長的,其實仔細看看,出去異常錯誤處理,這個方法主要做了三件事:
-
-
- 創建WebApplicationContext:上述代碼中
createWebApplicationContext(servletContext)
方法即是完成創建WebApplicationContext工作,也就是說這個方法創建上下文對象,支持用戶自定義上下文對象,但必須繼承ConfigurableWebApplicationContext,而Spring MVC默認使用ConfigurableWebApplicationContext作為ApplicationContext(它僅僅是一個接口)的實現。 - 加載對應的spring配置文件中的Bean:再往下走,有一個方法
configureAndRefreshWebApplicationContext
就是用來加載spring配置文件中的Bean實例的。這個方法於封裝ApplicationContext數據並且初始化所有相關Bean對象。它會從web.xml中讀取名為 contextConfigLocation的配置,這就是spring xml數據源設置,然后放到ApplicationContext中,最后調用傳說中的refresh
方法執行所有Java對象的創建。 - 將WebApplicationContext放入ServletContext(Java Web的全局變量)中:最后完成ApplicationContext創建之后就是將其放入ServletContext中,注意它存儲的key值常量。
- 創建WebApplicationContext:上述代碼中
-
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
