啟動Spring專案詳細過程(小結)
1、Spring 專案放到web專案容器中(Tomcat、Jetty、JBoss)
本文以通用的Tomcat為例
2、專案容器啟動時需要載入讀取web.xml配置檔案
如下圖:
3、容器首先會去讀取web.xml配置檔案中的兩個節點:<listener> </listener>和<context-param> </context-param>
說明:
tomcat在啟動web容器的時候會啟動一個叫ServletContextListener的監聽器,每當在web容器中有ServletContextListener這個介面被例項化的時候,web容器會通知ServletContextListener被例項的物件去執行其contextInitialized()的方法進行相應的業務處理;
而spring框架在設計的過程中ContextLoadListener這個類實現了ServletContextListener這個介面,因此每當有ContextLoadListener這個類被例項化的時候,web容器會通知Spring執行contextInitialized()這個方法,從而進行spring容器的啟動與建立的過程中;
4、ContextLoaderListener中的contextInitialized()進行了spring容器的啟動配置,呼叫initWebApplicationContext初始化spring容器;
@Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { //Spring 啟動的控制代碼,spring容器開始啟動的根目錄 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!"); } else { 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 { //處理spring容器是否已經建立(只建立沒有建立spring的各個bean) if(this.context == null) { this.context = this.createWebApplicationContext(servletContext); } if(this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)this.context; if(!cwac.isActive()) { if(cwac.getParent() == null) { ApplicationContext parent = this.loadParentContext(servletContext); cwac.setParent(parent); } //Spring容器建立完成後,載入spring容器的各個元件 this.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 var8) { logger.error("Context initialization failed",var8); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,var8); throw var8; } catch (Error var9) { logger.error("Context initialization failed",var9); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,var9); throw var9; } } }
5、spring容器建立完成後,準備開始例項化載入bean,Spring容器建立完成後,準備向spring容器中載入bean 使用configureAndRefreshWebApplicationContext(cwac,servletContext); 完成bean的載入;
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac,ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc,null); } customizeContext(sc,wac); wac.refresh(); }
說明:
configureAndRefreshWebApplicationContext中載入spring的配置檔案,即web.xml中讀取<context-param></context-param>中載入到Spring的配置檔案,即:classpath:/config/applicationContext.xml;
或
通過以下程式碼載入spring配置
public class Application{ public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/context.xml"); ctx.start(); } }
此處略過如何呼叫DefaultResourceLoader
頂級介面ResourceLoader僅提供了一個getResource(String location)方法,可以根據一個資源地址載入資原始檔,資源地址的表示式可以是以下幾種:
--1. classpath:字首開頭的表示式,例如: classpath:smart-context.xml
--2.“/”開頭的表示式,例如:/WEB-INF/classes/smart-context.xml
--3. 非“/”開頭的表達,例如:WEB-INF/classes/smart-context.xml
--4. url協議,例如:file:/D:/ALANWANG-AIA/Horse-workspace/chapter3/target/classes/smart-context.xml
Spring提供了實現類DefaultResourceLoader,DefaultResourceLoader在實現了以上列舉的功能基礎上,還為開發者提供了自定義擴充套件介面ProtocolResolver,開發者可實現該介面定製個性化資源表示式,程式碼如下:
@Override public Resource getResource(String location) { Assert.notNull(location,"Location must not be null"); for (ProtocolResolver protocolResolver : this.protocolResolvers) { // 1 Resource resource = protocolResolver.resolve(location,this); if (resource != null) {return resource;} } if (location.startsWith("/")) {return getResourceByPath(location);} //2 else if (location.startsWith(CLASSPATH_URL_PREFIX)) { //3 return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()),getClassLoader()); } else { try { // Try to parse the location as a URL... URL url = new URL(location); //4 return new UrlResource(url); } catch (MalformedURLException ex) { // No URL -> resolve as resource path. return getResourceByPath(location); //5 } } }
步驟1,先用擴充套件協議解析器解析資源地址並返回。舉個例子,咱們可以自定義資源解析器來完成帶字首“classpath:”的解析:
首先實現ProtocolResolver介面:
class ClasspathPreProtocolResolver implements ProtocolResolver{ private static String CLASS_PATH_PRE="classpath:"; public Resource resolve(String location,ResourceLoader resourceLoader) { if( location.startsWith(CLASS_PATH_PRE)) { return new ClassPathResource(location.substring(CLASS_PATH_PRE.length())); } return null; } }
步驟2,假設location以斜槓開頭,則呼叫該類中 getResourceByPath(String path)方法 ,程式碼如下:
protected Resource getResourceByPath(String path) { return new ClassPathContextResource(path,getClassLoader()); }
步驟三,假如資源表示式以classpath開頭,則擷取除字首calsspath:的路徑,並做為ClassPathResource的構造引數,生成ClassPathResource例項後返回。咱們可以在web.xml中做如下配置:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/config/applicationContext.xml</param-value> </context-param>
6、通過refresh()
內部的實現我們大致可以瞭解整個refresh()
方法擔負了整個Spring容器初始化和載入的所有邏輯,包括Bean工廠的初始化、post-processor的註冊以及呼叫、bean的例項化、事件釋出等。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。