亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

啟動Spring項目詳細過程(小結(jié))

 更新時間:2019年11月08日 09:48:01   作者:君寞傲  
這篇文章主要介紹了啟動Spring項目詳細過程(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、Spring 項目放到web項目容器中(Tomcat、Jetty、JBoss)

本文以通用的Tomcat為例

2、項目容器啟動時需要加載讀取web.xml配置文件

如下圖:

3、容器首先會去讀取web.xml配置文件中的兩個節(jié)點:<listener> </listener>和<context-param> </context-param>

說明:

tomcat在啟動web容器的時候會啟動一個叫ServletContextListener的監(jiān)聽器,每當(dāng)在web容器中有ServletContextListener這個接口被實例化的時候,web容器會通知ServletContextListener被實例的對象去執(zhí)行其contextInitialized()的方法進行相應(yīng)的業(yè)務(wù)處理;

而spring框架在設(shè)計的過程中ContextLoadListener這個類實現(xiàn)了ServletContextListener這個接口,因此每當(dāng)有ContextLoadListener這個類被實例化的時候,web容器會通知Spring執(zhí)行contextInitialized()這個方法,從而進行spring容器的啟動與創(chuàng)建的過程中;

4、ContextLoaderListener中的contextInitialized()進行了spring容器的啟動配置,調(diào)用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容器是否已經(jīng)創(chuàng)建(只創(chuàng)建沒有創(chuàng)建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容器創(chuàng)建完成后,加載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容器創(chuàng)建完成后,準(zhǔn)備開始實例化加載bean,Spring容器創(chuàng)建完成后,準(zhǔn)備向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();
 }
}

此處略過如何調(diào)用DefaultResourceLoader

頂級接口ResourceLoader僅提供了一個getResource(String location)方法,可以根據(jù)一個資源地址加載資源文件,資源地址的表達式可以是以下幾種:

--1. classpath:前綴開頭的表達式,例如: classpath:smart-context.xml

--2.“/”開頭的表達式,例如:/WEB-INF/classes/smart-context.xml

--3. 非“/”開頭的表達,例如:WEB-INF/classes/smart-context.xml

--4. url協(xié)議,例如:file:/D:/ALANWANG-AIA/Horse-workspace/chapter3/target/classes/smart-context.xml

Spring提供了實現(xiàn)類DefaultResourceLoader,DefaultResourceLoader在實現(xiàn)了以上列舉的功能基礎(chǔ)上,還為開發(fā)者提供了自定義擴展接口ProtocolResolver,開發(fā)者可實現(xiàn)該接口定制個性化資源表達式,代碼如下:

@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,先用擴展協(xié)議解析器解析資源地址并返回。舉個例子,咱們可以自定義資源解析器來完成帶前綴“classpath:”的解析:

首先實現(xiàn)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,假設(shè)location以斜杠開頭,則調(diào)用該類中 getResourceByPath(String path)方法 ,代碼如下:

protected Resource getResourceByPath(String path) {
		return new ClassPathContextResource(path, getClassLoader());
	}

步驟三,假如資源表達式以classpath開頭,則截取除前綴calsspath:的路徑,并做為ClassPathResource的構(gòu)造參數(shù),生成ClassPathResource實例后返回。咱們可以在web.xml中做如下配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/config/applicationContext.xml</param-value>
</context-param>

6、通過refresh()內(nèi)部的實現(xiàn)我們大致可以了解整個refresh()方法擔(dān)負了整個Spring容器初始化和加載的所有邏輯,包括Bean工廠的初始化、post-processor的注冊以及調(diào)用、bean的實例化、事件發(fā)布等。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot解決jar包沖突的問題,簡單有效

    SpringBoot解決jar包沖突的問題,簡單有效

    這篇文章主要介紹了SpringBoot解決jar包沖突的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java微信授權(quán)登陸的實現(xiàn)示例

    Java微信授權(quán)登陸的實現(xiàn)示例

    微信授權(quán)登錄,官方文檔寫的比較簡潔。所以對于會的人一目了然,而對于新手剛?cè)腴T的人來說是舉步維艱。本文詳細的介紹了Java微信授權(quán)登陸的實現(xiàn)示例,感興趣的朋友可以了解一下
    2021-06-06
  • Java實現(xiàn)飛機大戰(zhàn)-II游戲詳解

    Java實現(xiàn)飛機大戰(zhàn)-II游戲詳解

    《飛機大戰(zhàn)-II》是一款融合了街機、競技等多種元素的經(jīng)典射擊手游。游戲是用java語言實現(xiàn),采用了swing技術(shù)進行了界面化處理,感興趣的可以了解一下
    2022-02-02
  • Java基礎(chǔ)之?dāng)?shù)組超詳細知識總結(jié)

    Java基礎(chǔ)之?dāng)?shù)組超詳細知識總結(jié)

    這篇文章主要介紹了Java基礎(chǔ)之?dāng)?shù)組詳解,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Java基于ShardingSphere實現(xiàn)分庫分表的實例詳解

    Java基于ShardingSphere實現(xiàn)分庫分表的實例詳解

    ShardingSphere?已于2020年4月16日成為?Apache?軟件基金會的頂級項目,?它們均提供標(biāo)準(zhǔn)化的數(shù)據(jù)水平擴展、分布式事務(wù)和分布式治理等功能,可適用于如?Java?同構(gòu)、異構(gòu)語言、云原生等各種多樣化的應(yīng)用場景,對ShardingSphere分庫分表相關(guān)知識感興趣的朋友一起看看吧
    2022-03-03
  • Java學(xué)習(xí)之線程同步與線程間通信詳解

    Java學(xué)習(xí)之線程同步與線程間通信詳解

    這篇文章主要為大家詳細介紹了線程同步和線程之間的通信的相關(guān)知識,文中的示例代碼講解詳細,對我們學(xué)習(xí)Java有一定的幫助,感興趣的可以了解一下
    2022-12-12
  • JAVA正則表達式校驗qq號碼的方法

    JAVA正則表達式校驗qq號碼的方法

    Java作為一種開發(fā)語言,有許多值得推薦的地方,但是它一直以來沒有自帶對正則表達式的支持。下面小編給大家?guī)砹薐AVA正則表達式校驗qq號碼的方法,需要的朋友參考下吧
    2018-04-04
  • 詳細分析JAVA加解密算法

    詳細分析JAVA加解密算法

    這篇文章主要介紹了JAVA加解密算法的的相關(guān)資料,文中講解非常詳細,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Java+Spring+MySql環(huán)境中安裝和配置MyBatis的教程

    Java+Spring+MySql環(huán)境中安裝和配置MyBatis的教程

    這篇文章主要介紹了Java+Spring+MySql環(huán)境中安裝和配置MyBatis的教程,MyBatis一般被用來增強數(shù)據(jù)庫操作,文中對MyBatis的主配置文件有較為詳細的講解,需要的朋友可以參考下
    2016-04-04
  • SpringBoot實現(xiàn)文件上傳下載功能小結(jié)

    SpringBoot實現(xiàn)文件上傳下載功能小結(jié)

    最近做的一個項目涉及到文件上傳與下載功能。SpringBoot后臺如何實現(xiàn)文件上傳下載呢?下面有單文件上傳和多文件上傳功能,感興趣的朋友一起看看吧
    2017-08-08

最新評論