日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot————Spring Boot启动流程分析

發布時間:2025/3/12 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot————Spring Boot启动流程分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、引言

Spring Boot 的啟動雖然僅僅是執行了一個main方法,但實際上,運行流程還是比較復雜的,其中包含幾個非常重要的事件回調機制。在實際生產開發中,有時候也會利用這些啟動流程中的回調機制,做一些項目初始化的工作,比如內存初始化等。所以,學習Spring Boot啟動流程非常重要。

二、啟動流程概述

SpringApplication.run(Object, String...)方法的執行中包括以下一些關鍵步驟:

1、準備環境:?

  • 執行ApplicationContextInitializer.initialize()
  • 監聽器SpringApplicationRunListener回調contextPrepared()
  • 加載主配置類(啟動類)定義信息
  • 監聽器SpringApplicationRunListener回調contextLoaded()
  • 2、刷新啟動IOC容器:?

  • 掃描加載所有容器中的組件
  • 從META-INF/spring.factories中獲取所有的EnableAutoConfiguration組件
  • 3、回調容器中所有的 ApplicationRunner 、CommandLineRunner 的 run() 方法

    4、監聽器 SpringApplicationRunListener 回調 finished()方法

    三、詳細流程剖析

    Spring Boot的啟動方法調用流程為兩步:1、創建SpringApplication對象;2、執行run()方法

    這句代碼是一個中間的調用過程,接下來,我們將深度講解創建SpringApplication對象和執行run()方法具體都做了哪些工作。?

    1、創建SpringApplication對象

    通過SpringApplication的構造器,調用initialize()方法,對SpringApplication中的一些屬性初始化默認值,同時,從META-INF/spring.factories找到所有ApplicationContextInitializer和ApplicationListener保存起來。

    @SuppressWarnings({ "unchecked", "rawtypes" }) private void initialize(Object[] sources) {// 保存主配置類if (sources != null && sources.length > 0) {this.sources.addAll(Arrays.asList(sources));}// 判斷當前應用是否為一個WEB應用this.webEnvironment = deduceWebEnvironment();// 從類路徑下找到META-INF/spring.factories配置的所有ApplicationContextInitializer,然后保存起來setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));// 從類路徑下找到META-INF/spring.factories配置的所有ApplicationListenersetListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));// 從多個主配置類中找到有main方法的主配置類this.mainApplicationClass = deduceMainApplicationClass(); }

    2、運行run(String...)方法

    主要做了兩件事:

    • 回調:ApplicationContextInitializer和SpringApplicationRunListener;
    • 回調:ApplicationRunner和CommandLineRunner。

    詳細流程注釋如下:

    /*** Run the Spring application, creating and refreshing a new* {@link ApplicationContext}.* * @param args* the application arguments (usually passed from a Java main* method)* @return a running {@link ApplicationContext}*/ public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;FailureAnalyzers analyzers = null;configureHeadlessProperty();// 獲取SpringApplicationRunListeners,從類路徑下META-INF/spring-factoriesSpringApplicationRunListeners listeners = getRunListeners(args);// 循環所有的listener,回調starting()方法listeners.starting();try {// 封裝命令行參數ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);/** 準備環境:* 創建環境完成后回調SpringApplicationRunListener.environmentPrepared()方法,表示* 環境準備完成。*/ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);// 打印控制臺的Spring 字符畫Banner printedBanner = printBanner(environment);// 創建IOC容器:ApplicationContext;決定創建web IOC還是普通的IOC容器。context = createApplicationContext();// 創建錯誤分析對象analyzers = new FailureAnalyzers(context);/** 準備上下文環境,將environment保存到IOC容器中; 而且applyInitializers(),* 回調之前保存的所有的applicationContextInitializer的initialize()方法;* 回調所有的SpringApplicationRunListener的contextPrepareded();最后回調* 所有的SpringApplicationRunListener的contextLoaded()方法*/prepareContext(context, environment, listeners, applicationArguments, printedBanner);/** 刷新容器:IOC容器的初始化(掃描所有的配置類、@Bean等,加載并創建IOC容器中所有的組件。如果是web應用,* 還會創建嵌入式的tomcat),當執行完refreshContext()后,IOC容器即創建完畢*/refreshContext(context);/** 從IOC容器中獲取所有的ApplicationRunner和CommandLineRunner,* 然后先回調ApplicationRunner 再回調 CommandLineRunner*/afterRefresh(context, applicationArguments);// 所有的SpringApplicationRunListener回調finished()方法listeners.finished(context, null);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}// 整個Spring Boot應用啟動完成后,返回IOC容器return context;} catch (Throwable ex) {handleRunFailure(context, listeners, analyzers, ex);throw new IllegalStateException(ex);} }

    四、總結

    Spring Boot 的啟動流程是:

    1、準備環境

    2、刷新啟動IOC容器:?

    3、回調容器中所有的 ApplicationRunner 、CommandLineRunner 的 run() 方法

    4、監聽器 SpringApplicationRunListener 回調 finished()方法

    在“詳細啟動流程”?中,已經將run()方法中實際執行流程用注釋的方式標記出來了,里面的方法都通過調用的方式完成了一些特定的功能,最主要的是把握他們的執行順序和完成內容,可以通過debug的方式,并觀察控制臺輸出和參數內容來進行追蹤學習。

    綜上,就是關于Spring Boot啟動的完整流程,歡迎文末留言。

    總結

    以上是生活随笔為你收集整理的Spring Boot————Spring Boot启动流程分析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。