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

java?Spring的啟動(dòng)原理詳解

 更新時(shí)間:2022年01月29日 10:25:30   作者:Java技術(shù)債務(wù)  
大家好,本篇文章主要講的是java?Spring的啟動(dòng)原理詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下

引入

為什么突然說(shuō)一下Spring啟動(dòng)原理呢,因?yàn)橹懊嬖嚨臅r(shí)候,回答的那可謂是坑坑洼洼,前前后后,補(bǔ)補(bǔ)貼貼。。。

總而言之就是不行,再次看一下源碼發(fā)掘一下。。。

在Spring Boot還沒有廣泛到家家在用的時(shí)候,我們都還在書寫繁瑣的配置,什么web.xml、spring.xml、bean.xml等等。雖然現(xiàn)在很少,可以說(shuō)幾乎沒有企業(yè)在去使用Spring的老一套,而會(huì)去使用Spring Boot約定大于配置來(lái)進(jìn)行快速開發(fā),但是,Spring的也要去學(xué)習(xí),去挖掘,畢竟是我們Java程序員的基礎(chǔ)呀。

spring的啟動(dòng)是建筑在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和監(jiān)聽器(Listener)

web.xml

<!--上下文監(jiān)聽器,用于監(jiān)聽servlet的啟動(dòng)過(guò)程-->
    <listener>
        <description>ServletContextListener</description>
        <!--這里是自定義監(jiān)聽器,個(gè)性化定制項(xiàng)目啟動(dòng)提示-->
        <listener-class>com.trace.app.framework.listeners.ApplicationListener</listener-class>
    </listener>
    <!--dispatcherServlet的配置,這個(gè)servlet主要用于前端控制,這是springMVC的基礎(chǔ)-->
    <servlet>
        <servlet-name>service_dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/services/service_dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--spring資源上下文定義,在指定地址找到spring的xml配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/application_context.xml</param-value>
    </context-param>
    <!--spring的上下文監(jiān)聽器-->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!--Session監(jiān)聽器,Session作為公共資源存在上下文資源當(dāng)中,這里也是自定義監(jiān)聽器-->
    <listener>
        <listener-class>
            com.trace.app.framework.listeners.MySessionListener
        </listener-class>
    </listener>

Spring啟動(dòng)過(guò)程

spring的上下文監(jiān)聽器

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/application_context.xml</param-value>
</context-param>

<listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
 </listener>

spring的啟動(dòng)其實(shí)就是IOC容器的啟動(dòng)過(guò)程,通過(guò)上述的第一段配置 <context-param> 是初始化上下文,然后通過(guò)后一段的的<listener>來(lái)加載配置文件,其中調(diào)用的spring包中的ContextLoaderListener這個(gè)上下文監(jiān)聽器,ContextLoaderListener是一個(gè)實(shí)現(xiàn)了ServletContextListener接口的監(jiān)聽器,他的父類是 ContextLoader,在啟動(dòng)項(xiàng)目時(shí)會(huì)觸發(fā)contextInitialized上下文初始化方法。

public void contextInitialized(ServletContextEvent event) {
        initWebApplicationContext(event.getServletContext());
}

調(diào)用了父類ContextLoader的initWebApplicationContext(event.getServletContext());方法,很顯然,這是對(duì)ApplicationContext的初始化方法,也就是到這里正是進(jìn)入了springIOC的初始化。

接下來(lái)看一下initWebApplicationContext(event.getServletContext())的工作:

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;
        }

總結(jié):

創(chuàng)建WebApplicationContext。加載對(duì)應(yīng)的spring配置文件中的Bean。將WebApplicationContext放入ServletContext(Java Web的全局變量)中。

接下來(lái),來(lái)到了configureAndRefreshWebApplicationContext()方法

作用:

就是用來(lái)加載spring配置文件中的Bean實(shí)例的。這個(gè)方法于封裝ApplicationContext數(shù)據(jù)并且初始化所有相關(guān)Bean對(duì)象。它會(huì)從web.xml中讀取名為 contextConfigLocation的配置,這就是spring xml數(shù)據(jù)源設(shè)置,然后放到ApplicationContext中,最后調(diào)用傳說(shuō)中的refresh方法執(zhí)行所有Java對(duì)象的創(chuàng)建。

總結(jié):

在這里插入圖片描述

總結(jié)

首先對(duì)于一個(gè)web應(yīng)用,需要部署到web容器中,web容器提供了一個(gè)全局的上下文環(huán)境,ServletContext,SpringIOC的宿主環(huán)境。

其次,在web容器啟動(dòng)時(shí),觸發(fā)容器初始化,web.xml中提供的有ContextLoaderListener監(jiān)聽器會(huì)監(jiān)聽這個(gè)事件,初始化方法contextInitialized被調(diào)用,初始化spring上下文
WebApplicationContext接口,實(shí)現(xiàn)類時(shí)XmlWebApplicationContext即SpringIOC容器,對(duì)應(yīng)的Bean定義是有context-param標(biāo)簽定義指定,然后存儲(chǔ)到ServletContext中,方便獲取。

ContextLoaderListener監(jiān)聽初始化完成后,開始初始化web.xml中配置的Servlet,指DisapatchServlet前端控制器,用來(lái)匹配,轉(zhuǎn)發(fā),處理每個(gè)Servlet請(qǐng)求,DisaptchServlet初始化時(shí)會(huì)創(chuàng)建自己的IOC上下文,用來(lái)持有Spring MVC的相關(guān)bean。
首先會(huì)從之前初始化存儲(chǔ)在ServletContext中的上下文左右parent上下文,再初始化自己的上下文,大概的工作就是初始化處理器映射、視圖解析等。這個(gè)servlet自己持有的上下文默認(rèn)實(shí)現(xiàn)類也是xmlWebApplicationContext。然后存儲(chǔ)到ServletContext。每個(gè)Servlet擁有自己的上下文,也會(huì)共享parent的上下文。

到此這篇關(guān)于java Spring的啟動(dòng)原理詳解的文章就介紹到這了,更多相關(guān)java Spring啟動(dòng)原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • selenium+java+chrome環(huán)境搭建的方法步驟

    selenium+java+chrome環(huán)境搭建的方法步驟

    這篇文章主要介紹了selenium+java+chrome環(huán)境搭建的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Knife4j?3.0.3?整合SpringBoot?2.6.4的詳細(xì)過(guò)程

    Knife4j?3.0.3?整合SpringBoot?2.6.4的詳細(xì)過(guò)程

    本文要講的是?Knife4j?3.0.3?整合SpringBoot?2.6.4,在SpringBoot?2.4以上的版本和之前的版本還是不一樣的,這個(gè)也容易導(dǎo)致一些問(wèn)題,本文就這兩個(gè)版本的整合做一個(gè)實(shí)戰(zhàn)介紹
    2022-09-09
  • spring boot 如何優(yōu)雅關(guān)閉服務(wù)

    spring boot 如何優(yōu)雅關(guān)閉服務(wù)

    這篇文章主要介紹了spring boot 如何優(yōu)雅關(guān)閉服務(wù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Spring Boot實(shí)現(xiàn)熱部署的實(shí)例方法

    Spring Boot實(shí)現(xiàn)熱部署的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于Spring Boot實(shí)現(xiàn)熱部署的實(shí)例方法和實(shí)例,需要的朋友們可以參考下。
    2020-02-02
  • java selenium Selenium IDE介紹及用法

    java selenium Selenium IDE介紹及用法

    本文主要介紹java selenium Selenium IDE,這里整理了相關(guān)資料和介紹如何安裝 Selenium IDE和使用方法,有需要的小伙伴可以參考下
    2016-08-08
  • Netty分布式高性能工具類recycler的使用及創(chuàng)建

    Netty分布式高性能工具類recycler的使用及創(chuàng)建

    這篇文章主要為大家介紹了Netty分布式高性能工具類recycler的使用和創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • java實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放器

    java實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放器

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Spring Security 多過(guò)濾鏈的使用詳解

    Spring Security 多過(guò)濾鏈的使用詳解

    本文主要介紹了Spring Security 多過(guò)濾鏈的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(61)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(61)

    下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你
    2021-08-08
  • 一次java異步任務(wù)的實(shí)戰(zhàn)記錄

    一次java異步任務(wù)的實(shí)戰(zhàn)記錄

    最近做項(xiàng)目的時(shí)候遇到了一個(gè)小問(wèn)題,從前臺(tái)提交到服務(wù)端A,A調(diào)用服務(wù)端B處理超時(shí),下面這篇文章主要給大家介紹了一次java異步任務(wù)的實(shí)戰(zhàn)記錄,需要的朋友可以參考下
    2022-05-05

最新評(píng)論

嫩江县| 屯门区| 台湾省| 阿拉善盟| 西林县| 康平县| 崇仁县| 武清区| 保亭| 嵊州市| 莱芜市| 潼关县| 安宁市| 西城区| 内黄县| 黑龙江省| 杭锦后旗| 阳江市| 麻江县| 航空| 新乡县| 徐州市| 石河子市| 巴彦县| 九江市| 五台县| 峨眉山市| 油尖旺区| 霍邱县| 会同县| 浦北县| 民丰县| 乌拉特中旗| 炎陵县| 高雄市| 尉犁县| 福清市| 安远县| 太仆寺旗| 芒康县| 桐梓县|