欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot詳解執(zhí)行過程

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

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

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

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

@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()方法內部執(zhí)行了兩個操作,分別是SpringApplication實例的初始化創(chuàng)建和調用run()啟動項目,這兩個階段的實現具體說明如下

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設置為屬性存儲起來
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    // 判斷當前webApplicationType應用的類型
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
    // 設置初始化器(Initializer),最后會調用這些初始化器
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    // 設置監(jiān)聽器(Listener)
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    // 用于推斷并設置項目main()方法啟動的主程序啟動類
        this.mainApplicationClass = this.deduceMainApplicationClass();

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

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

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

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

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

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

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

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

用于推斷并設置項目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);
    // 第二步:根據SpringApplicationRunListeners以及參數來準備環(huán)境
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
    // 準備Banner打印器 - 就是啟動Spring Boot的時候打印在console上的ASCII藝術字體
            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ā)出結束執(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)聽器并運行。

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

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

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

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

第四步:Spring容器前置處理

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

第五步:刷新容器

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

第六步:Spring容器后置處理

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

第七步:發(fā)出結束執(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

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

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

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

相關文章

  • SpringBoot使用Swagger范例講解

    SpringBoot使用Swagger范例講解

    Swagger是一個規(guī)范和完整的框架,用于生成、描述、調用和可視化 Restful 風格的 Web 服務??傮w目標是使客戶端和文件系統作為服務器以同樣的速度來更新。文件的方法、參數和模型緊密集成到服務器端的代碼,允許API來始終保持同步
    2022-07-07
  • htmlcleaner使用方法及xpath語法初探

    htmlcleaner使用方法及xpath語法初探

    HtmlCleaner是一個開源的Java語言的Html文檔解析器。HtmlCleaner能夠重新整理HTML文檔的每個元素并生成結構良好(Well-Formed)的 HTML 文檔
    2015-08-08
  • 淺談一下Java中的悲觀鎖和樂觀鎖

    淺談一下Java中的悲觀鎖和樂觀鎖

    這篇文章主要介紹了一下Java中的悲觀鎖和樂觀鎖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • SpringBoot注解@CrossOrigin使用詳解

    SpringBoot注解@CrossOrigin使用詳解

    這篇文章主要介紹了SpringBoot注解@CrossOrigin使用詳解,@CrossOrigin是用來處理跨域請求的注解
    跨域,指的是瀏覽器不能執(zhí)行其他網站的腳本,它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript施加的安全限制,需要的朋友可以參考下
    2023-12-12
  • springboot運行到dokcer中 dockerfile的場景分析

    springboot運行到dokcer中 dockerfile的場景分析

    這篇文章主要介紹了springboot運行到dokcer中 dockerfile,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 基于Java實現Avro文件讀寫功能

    基于Java實現Avro文件讀寫功能

    大家好,本篇文章主要講的是基于Java實現Avro文件讀寫功能,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Request的包裝類HttpServletRequestWrapper的使用說明

    Request的包裝類HttpServletRequestWrapper的使用說明

    這篇文章主要介紹了Request的包裝類HttpServletRequestWrapper的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java實現猜數字游戲

    java實現猜數字游戲

    這篇文章主要為大家詳細介紹了java實現猜數字游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java?深入探究講解簡單工廠模式

    Java?深入探究講解簡單工廠模式

    簡單工廠模式是屬于創(chuàng)建型模式,又叫做靜態(tài)工廠方法(Static Factory Method)模式,但不屬于23種GOF設計模式之一。簡單工廠模式是由一個工廠對象決定創(chuàng)建出哪一種產品類的實例。簡單工廠模式是工廠模式家族中最簡單實用的模式,可以理解為是不同工廠模式的一個特殊實現
    2022-04-04
  • java從list中取出對象并獲得其屬性值的方法

    java從list中取出對象并獲得其屬性值的方法

    這篇文章主要介紹了java從list中取出對象并獲得其屬性值的方法,大家參考使用
    2013-12-12

最新評論