最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中ContextLoaderListener監(jiān)聽詳解

 更新時間:2024年01月05日 08:59:48   作者:it_lihongmin  
這篇文章主要介紹了Spring中ContextLoaderListener監(jiān)聽詳解,SpringMVC啟動時會啟動WebApplicationContext類型的容器,并且會調(diào)用之前分析的refresh方法,需要的朋友可以參考下

前言

web.xml配置文件的部分,方便下面的理解:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:Resource/SpringConf.xml</param-value>
</context-param>
  
  
<servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
	<init-param>  
		<param-name>contextConfigLocation</param-name>  
		<param-value>/WEB-INF/springweb.xml</param-value>  
	</init-param>  
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

一、SpringMVC總覽

SpringMVC啟動時會啟動WebApplicationContext類型的容器,并且會調(diào)用之前分析的refresh方法。

還是從原始的SpringMVC開始,啟動方式有三種,配置文件中配置ContextLoadServlet,ContextLoaderListener、ContextLoaderPlugin。

不論哪種方式啟動都大致相同。主要包括兩部分:

1、ContextLoaderListener監(jiān)聽

初始化Spring容器,WebApplicationContext類型,并且執(zhí)行refresh方法

2、DispatcherServlet(一個特殊的Servlet,如上面的配置web.xml中進行配置了)

1)、DispatcherServlet像普通Servlet一樣,init中啟動九大件為服務(wù)請求做準備

HttpServletBean#init => DispatcherServlet#onRefresh => DispatcherServlet#initStrategies(初始化九大件)

2)、當上面初始化完成后,則請求進入后攔截轉(zhuǎn)發(fā)到DispatcherServlet

DispatcherServlet#doService=> DispatcherServlet#doDispatch(執(zhí)行MVC流程)

二、ContextLoaderListener的contextInitialized監(jiān)聽事件

當Web容器啟動時,觸發(fā)ContextLoaderListenercontextInitialized方法:

@Override
public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
}
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
    // 省略部分日志和try catch代碼
    long startTime = System.currentTimeMillis();
    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);
    }
    return this.context;
}

這里需要考慮容器是否其他地方已經(jīng)創(chuàng)建了,但是不論是否創(chuàng)建。整個Spring MVC啟動過程需要經(jīng)歷幾個步驟:

1、createWebApplicationContext(創(chuàng)建Web類型的ApplicationContext)

2、configureAndRefreshWebApplicationContext(主要是refresh方法執(zhí)行)

3、將初始化的WebApplicationContext容器,用key為ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE加載到ServletContext容器中

創(chuàng)建Web類型的ApplicationContext

protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
    Class<?> contextClass = determineContextClass(sc);
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("省略");
    }
    return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}

先判斷啟動的WebApplicationContext的類型Class,再使用反射初始化。主要是看啟動的Class是怎么確定的。

protected Class<?> determineContextClass(ServletContext servletContext) {
    String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
    if (contextClassName != null) {
        // 省略try catch代碼
        return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
    }
    else {
        contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
        // 省略try catch代碼
        return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
    }
}

先看啟動Web時是否有進行配置Class,否則就在默認的配置策略中進行獲取。詳細可以參見:Spring中的策略模式簡單實現(xiàn)與使用分析

從配置文件ContextLoader.properties中獲取到的類型為(所以如果沒有配置默認啟動XmlWebApplicationContext,Spring Boot不會啟動該類型,但是SpringMVC沒有太大差異):

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

執(zhí)行refresh方法

不管怎么樣都回執(zhí)行configureAndRefreshWebApplicationContext方法,以啟動Spring 容器,也就是之前一直分析refresh方法。

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, 
                                                        ServletContext sc) {
    // 設(shè)置ConfigurableWebApplicationContext的容器Id
    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()));
        }
    }
    // 設(shè)置Web容器上下文,設(shè)置到Spring容器中
    wac.setServletContext(sc);
    String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
    // 配置location,如果配置的話,則refresh方法時會調(diào)用Resource加載,解析,初始化
    if (configLocationParam != null) {
        wac.setConfigLocation(configLocationParam);
    }
    // 設(shè)置Environment,refresh時也分析過,如果沒有則會從java.lang.System中
    // 獲取數(shù)據(jù)初始化StandardEnvironment或其子類
    ConfigurableEnvironment env = wac.getEnvironment();
    if (env instanceof ConfigurableWebEnvironment) {
        ((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
    }
    // 初始化自定義的容器,如果自己配置的話(我們可以自定義實現(xiàn)ServletContextListener)
    customizeContext(sc, wac);
    // 調(diào)用refresh方法
    wac.refresh();
}

1、設(shè)置WebApplicationContext的Id

2、設(shè)置Web容器上下文,設(shè)置到Spring容器中

3、配置location(比如上面配置中的contextConfigLocation,則refresh方法時會調(diào)用Resource加載,解析,初始化)

4、初始化自定義的監(jiān)聽

5、最重要的一步,啟動Spring容器(可以從這里開始看:SpringIoc源碼(五)- ApplicationContext(一)- 結(jié)構(gòu)梳理)

到此這篇關(guān)于Spring中ContextLoaderListener監(jiān)聽詳解的文章就介紹到這了,更多相關(guān)ContextLoaderListener監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PowerJob分布式任務(wù)調(diào)度源碼流程解讀

    PowerJob分布式任務(wù)調(diào)度源碼流程解讀

    這篇文章主要為大家介紹了PowerJob分布式任務(wù)調(diào)度源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-02-02
  • java常用工具類 Date日期、Mail郵件工具類

    java常用工具類 Date日期、Mail郵件工具類

    這篇文章主要為大家詳細介紹了java常用工具類,包括Date日期、Mail郵件工具類,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 各系統(tǒng)下多版本JDK安裝與配置步驟

    各系統(tǒng)下多版本JDK安裝與配置步驟

    這篇文章主要介紹了各系統(tǒng)下多版本JDK安裝與配置的相關(guān)資料,包括Windows、Mac和Linux系統(tǒng)下的具體步驟,文中通過代碼將步驟介紹的非常詳細,需要的朋友可以參考下
    2026-02-02
  • java時間戳與日期相互轉(zhuǎn)換工具詳解

    java時間戳與日期相互轉(zhuǎn)換工具詳解

    這篇文章主要為大家詳細介紹了java各種時間戳與日期之間相互轉(zhuǎn)換的工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • SpringBoot bean查詢加載順序流程詳解

    SpringBoot bean查詢加載順序流程詳解

    當你在項目啟動時需要提前做一個業(yè)務(wù)的初始化工作時,或者你正在開發(fā)某個中間件需要完成自動裝配時。你會聲明自己的Configuration類,但是可能你面對的是好幾個有互相依賴的Bean
    2023-03-03
  • FileOutputStream文件寫入類實現(xiàn)方式

    FileOutputStream文件寫入類實現(xiàn)方式

    這篇文章主要介紹了FileOutputStream文件寫入類實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • ActiveMQ中consumer的消息確認機制詳解

    ActiveMQ中consumer的消息確認機制詳解

    這篇文章主要介紹了ActiveMQ中consumer的消息確認機制詳解,對于broker而言,只有接收到確認指令,才會認為消息被正確的接收或者處理成功了,InforSuiteMQ提供以下幾種Consumer與Broker之間的消息確認方式,需要的朋友可以參考下
    2023-10-10
  • SpringData JPA中@OneToMany和@ManyToOne的用法詳解

    SpringData JPA中@OneToMany和@ManyToOne的用法詳解

    這篇文章主要介紹了SpringData JPA中@OneToMany和@ManyToOne的用法詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot 應用程序測試實現(xiàn)方案

    SpringBoot 應用程序測試實現(xiàn)方案

    這篇文章主要介紹了SpringBoot 應用程序測試實現(xiàn)方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java手機號最新校驗規(guī)則

    Java手機號最新校驗規(guī)則

    在Java中,進行手機號校驗通常使用正則表達式(Regex)來匹配手機號的格式,以下是一個基于當前(截至2024年)中國手機號規(guī)則的校驗方法,感興趣的朋友跟隨小編一起看看吧
    2024-05-05

最新評論

松阳县| 根河市| 兖州市| 嘉黎县| 温州市| 新泰市| 永寿县| 太保市| 兴业县| 阿尔山市| 宝鸡市| 沙河市| 铜陵市| 鄂州市| 大同市| 班玛县| 广德县| 土默特右旗| 昌乐县| 彭泽县| 清水河县| 德惠市| 精河县| 镇江市| 怀安县| 龙里县| 淄博市| 西乌珠穆沁旗| 全州县| 赫章县| 卢湾区| 蓝田县| 德兴市| 荥经县| 乌海市| 孝感市| 乌拉特后旗| 前郭尔| 久治县| 乌拉特前旗| 双牌县|