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

SpringBoot詳解執(zhí)行過程

 更新時間:2022年07月15日 11:11:10   作者:悠然予夏  
這篇文章主要介紹了SpringBoot的執(zhí)行過程原理,Spring Boot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

每個Spring Boot項目都有一個主程序啟動類,在主程序啟動類中有一個啟動項目的main()方法,在該方法中通過執(zhí)行SpringApplication.run()即可啟動整個Spring Boot程序。

問題:那么SpringApplication.run()方法到底是如何做到啟動Spring Boot項目的呢?

下面我們查看run()方法內(nèi)部的源碼,核心代碼具體如下:

@SpringBootApplication
public class SpringbootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }
}
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class[]{primarySource}, args);
}
    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return (new SpringApplication(primarySources)).run(args);
}

從上述源碼可以看出,SpringApplication.run()方法內(nèi)部執(zhí)行了兩個操作,分別是SpringApplication實例的初始化創(chuàng)建和調(diào)用run()啟動項目,這兩個階段的實現(xiàn)具體說明如下

1.SpringApplication實例的初始化創(chuàng)建

查看SpringApplication實例對象初始化創(chuàng)建的源碼信息,核心代碼具體如下

    public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.isCustomEnvironment = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
    // 把項目啟動類.class設(shè)置為屬性存儲起來
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    // 判斷當(dāng)前webApplicationType應(yīng)用的類型
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
    // 設(shè)置初始化器(Initializer),最后會調(diào)用這些初始化器
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    // 設(shè)置監(jiān)聽器(Listener)
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    // 用于推斷并設(shè)置項目main()方法啟動的主程序啟動類
        this.mainApplicationClass = this.deduceMainApplicationClass();

從上述源碼可以看出,SpringApplication的初始化過程主要包括4部分,具體說明如下。

(1)this.webApplicationType = WebApplicationType.deduceFromClasspath()

用于判斷當(dāng)前webApplicationType應(yīng)用的類型。deduceFromClasspath()方法用于查看Classpath類路徑下是否存在某個特征類,從而判斷當(dāng)前webApplicationType類型是SERVLET應(yīng)用(Spring 5之前的傳統(tǒng)MVC應(yīng)用)還是REACTIVE應(yīng)用(Spring 5開始出現(xiàn)的WebFlux交互式應(yīng)用)

(2)this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class))

用于SpringApplication應(yīng)用的初始化器設(shè)置。在初始化器設(shè)置過程中,會使用Spring類加載器SpringFactoriesLoader從META-INF/spring.factories類路徑下的META-INF下的spring.factores文件中獲取所有可用的應(yīng)用初始化器類ApplicationContextInitializer。

(3)this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class))

用于SpringApplication應(yīng)用的監(jiān)聽器設(shè)置。監(jiān)聽器設(shè)置的過程與上一步初始化器設(shè)置的過程基本一樣,也是使用SpringFactoriesLoader從META-INF/spring.factories類路徑下的META-INF下的spring.factores文件中獲取所有可用的監(jiān)聽器類ApplicationListener。

(4)this.mainApplicationClass = this.deduceMainApplicationClass()

用于推斷并設(shè)置項目main()方法啟動的主程序啟動類

2.項目的初始化啟動

分析完(new SpringApplication(primarySources)).run(args)源碼前一部分SpringApplication實例對象的初始化創(chuàng)建后,查看run(args)方法執(zhí)行的項目初始化啟動過程,核心代碼具體如下:

  public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
    // 第一步:獲取并啟動監(jiān)聽器
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();
        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    // 第二步:根據(jù)SpringApplicationRunListeners以及參數(shù)來準(zhǔn)備環(huán)境
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
    // 準(zhǔn)備Banner打印器 - 就是啟動Spring Boot的時候打印在console上的ASCII藝術(shù)字體
            Banner printedBanner = this.printBanner(environment);
    // 第三步:創(chuàng)建Spring容器
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, new Object[]{context});
    // 第四步:Spring容器前置處理
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    // 第五步:刷新容器
            this.refreshContext(context);
    // 第六步:Spring容器后置處理
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
    // 第七步:發(fā)出結(jié)束執(zhí)行的事件
            listeners.started(context);
    // 返回容器
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }
        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners) null);
            throw new IllegalStateException(var9);
        }
    }

從上述源碼可以看出,項目初始化啟動過程大致包括以下部分:

第一步:獲取并啟動監(jiān)聽器

this.getRunListeners(args)和listeners.starting()方法主要用于獲取SpringApplication實例初始化過程中初始化的SpringApplicationRunListener監(jiān)聽器并運行。

第二步:根據(jù)SpringApplicationRunListeners以及參數(shù)來準(zhǔn)備環(huán)境

this.prepareEnvironment(listeners, applicationArguments)方法主要用于對項目運行環(huán)境進行預(yù)設(shè)置,同時通過this.configureIgnoreBeanInfo(environment)方法排除一些不需要的運行環(huán)境

第三步:創(chuàng)建Spring容器

根據(jù)webApplicationType進行判斷, 確定容器類型,如果該類型為SERVLET類型,會通過反射裝載對應(yīng)的字節(jié)碼,也就是AnnotationConfigServletWebServerApplicationContext,接著使用之前初始化設(shè)置的context(應(yīng)用上下文環(huán)境)、environment(項目運行環(huán)境)、listeners(運行監(jiān)聽器)、applicationArguments(項目參數(shù))和printedBanner(項目圖標(biāo)信息)進行應(yīng)用上下文的組裝配置,并刷新配置

第四步:Spring容器前置處理

這一步主要是在容器刷新之前的準(zhǔn)備動作。設(shè)置容器環(huán)境,包括各種變量等等,其中包含一個非常關(guān)鍵的操作:將啟動類注入容器,為后續(xù)開啟自動化配置奠定基礎(chǔ)

第五步:刷新容器

開啟刷新spring容器,通過refresh方法對整個IOC容器的初始化(包括bean資源的定位,解析,注冊等等),同時向JVM運行時注冊一個關(guān)機鉤子,在JVM關(guān)機時會關(guān)閉這個上下文,除非當(dāng)時它已經(jīng)關(guān)閉

第六步:Spring容器后置處理

擴展接口,設(shè)計模式中的模板方法,默認(rèn)為空實現(xiàn)。如果有自定義需求,可以重寫該方法。比如打印一些啟動結(jié)束log,或者一些其它后置處理。

第七步:發(fā)出結(jié)束執(zhí)行的事件

獲取EventPublishingRunListener監(jiān)聽器,并執(zhí)行其started方法,并且將創(chuàng)建的Spring容器傳進去了,創(chuàng)建一個ApplicationStartedEvent事件,并執(zhí)行ConfigurableApplicationContext 的

publishEvent方法,也就是說這里是在Spring容器中發(fā)布事件,并不是在SpringApplication中發(fā)布事件,和前面的starting是不同的,前面的starting是直接向SpringApplication中的監(jiān)聽器發(fā)布啟動事件。

第八步:執(zhí)行Runners

用于調(diào)用項目中自定義的執(zhí)行器XxxRunner類,使得在項目啟動完成后立即執(zhí)行一些特定程序。其中,Spring Boot提供的執(zhí)行器接口有ApplicationRunner 和CommandLineRunner兩種,在使用時只需要自定義一個執(zhí)行器類實現(xiàn)其中一個接口并重寫對應(yīng)的run()方法接口,然后Spring Boot項目啟動后會立即執(zhí)行這些特定程序

下面,通過一個Spring Boot執(zhí)行流程圖,讓大家更清晰的知道Spring Boot的整體執(zhí)行流程和主要啟動階段:

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

相關(guān)文章

  • java實現(xiàn)支付寶支付接口的調(diào)用

    java實現(xiàn)支付寶支付接口的調(diào)用

    本文主要介紹了java實現(xiàn)支付寶支付接口的調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 如何把Spring Cloud Data Flow部署在Kubernetes上

    如何把Spring Cloud Data Flow部署在Kubernetes上

    這篇文章主要介紹了把Spring Cloud Data Flow部署在Kubernetes上,再跑個任務(wù)試試,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Java對象的內(nèi)存布局全流程

    Java對象的內(nèi)存布局全流程

    這篇文章主要介紹了Java對象的內(nèi)存布局全流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • spring boot接收請求常用注解示例詳解

    spring boot接收請求常用注解示例詳解

    這篇文章介紹了Spring Boot中常用的接收請求的注解,包括`@RequestBody`、`@PathVariable`、`@RequestParam`和`@DateTimeFormat`,并提供了每個注解的示例,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • SpringMVC請求、響應(yīng)和攔截器的使用實例詳解

    SpringMVC請求、響應(yīng)和攔截器的使用實例詳解

    攔截器(Interceptor) 它是一個Spring組件,并由Spring容器管理,并不依賴Tomcat等容器,是可以單獨使用的,這篇文章給大家介紹SpringMVC請求、響應(yīng)和攔截器的使用,感興趣的朋友一起看看吧
    2024-03-03
  • SpringBoot集成Flink-CDC實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)的監(jiān)聽問題

    SpringBoot集成Flink-CDC實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)的監(jiān)聽問題

    Flink CDC(Flink Change Data Capture)是一種基于數(shù)據(jù)庫日志的CDC技術(shù),它實現(xiàn)了一個全增量一體化的數(shù)據(jù)集成框架,這篇文章主要介紹了SpringBoot集成Flink-CDC,實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)的監(jiān)聽,需要的朋友可以參考下
    2024-07-07
  • Java開發(fā)常用類庫之Hutool詳解

    Java開發(fā)常用類庫之Hutool詳解

    這篇文章主要介紹了Java開發(fā)常用類庫之Hutool,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Spring Cloud Stream微服務(wù)消息框架原理及實例解析

    Spring Cloud Stream微服務(wù)消息框架原理及實例解析

    這篇文章主要介紹了Spring Cloud Stream微服務(wù)消息框架原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 在java中http請求帶cookie的例子

    在java中http請求帶cookie的例子

    今天小編就為大家分享一篇在java中http請求帶cookie的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • dubbo集成zipkin獲取Traceid的實現(xiàn)

    dubbo集成zipkin獲取Traceid的實現(xiàn)

    這篇文章主要介紹了dubbo集成zipkin獲取Traceid的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論

营口市| 定兴县| 夏河县| 瑞昌市| 寿光市| 浮山县| 偏关县| 乌兰县| 来安县| 达拉特旗| 汾西县| 大姚县| 句容市| 张家口市| 湖州市| 昌邑市| 黄梅县| 汉中市| 浦城县| 遂平县| 新津县| 衡水市| 榆林市| 日照市| 交口县| 张家口市| 玉溪市| 澎湖县| 梅州市| 武清区| 江北区| 泗水县| 左贡县| 东平县| 从江县| 清丰县| 竹北市| 永川市| 车险| 广东省| 寻甸|