SpringApplication到底run了什麼(上)

来源:https://www.cnblogs.com/zhixiang-org-cn/archive/2019/09/23/11575705.html
-Advertisement-
Play Games

在上篇文章: "SpringBoot源碼解析:創建SpringApplication對象實例" 中,我們詳細描述了SpringApplication對象實例的創建過程,本篇文章繼續看 方法的執行邏輯吧 1. 第一行使用了 來記錄開始時間 2. 設置了 環境變數,在網上瞭解了一下這個變數的相關信息 H ...


在上篇文章:SpringBoot源碼解析:創建SpringApplication對象實例中,我們詳細描述了SpringApplication對象實例的創建過程,本篇文章繼續看run方法的執行邏輯吧

    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            //後面還有,本篇文章就解析到這。。。。
    }
  1. 第一行使用了StopWatch來記錄開始時間
  2. 設置了java.awt.headless環境變數,在網上瞭解了一下這個變數的相關信息

    Headless模式是系統的一種配置模式。在系統可能缺少顯示設備、鍵盤或滑鼠這些外設的情況下可以使用該模式

    個人理解為是一些圖形相關的組件能否使用的開關,歡迎各位大佬指正

  3. 接著遍歷所有構造SpringApplication實例時載入的SpringApplicationRunListener,調用它們的started方法

這裡構造時僅僅載入了一個EventPublishingRunListener類,所以咱們就來解析一下這個東東

    public void starting() {
        this.initialMulticaster.multicastEvent(
                new ApplicationStartingEvent(this.application, this.args));
    }

可以看到這裡調用了SimpleApplicationEventMulticaster類的multicastEvent方法並且傳入了ApplicationStartingEvent對象,看名字就知道了這個是SpringBoot啟動事件


public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
        ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
        for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
            Executor executor = getTaskExecutor();
            if (executor != null) {
                executor.execute(() -> invokeListener(listener, event));
            }
            else {
                invokeListener(listener, event);
            }
        }
    }

其中獲取監聽器使用的是getApplicationListeners方法,這個方法中主要就是從最啟動時獲取的所有監聽器和這個事件做了下匹配,返回通過匹配的監聽器集合

接著就是看是否設置線程池參數,如果有線程池則使用線程池的線程進行操作,否則將同步調用監聽器

  1. 把所有的命令行啟動參數封裝成ConfigurableEnvironment對象
  2. 準備運行時環境
    private ConfigurableEnvironment prepareEnvironment(
            SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments) {
        ConfigurableEnvironment environment = getOrCreateEnvironment();
        configureEnvironment(environment, applicationArguments.getSourceArgs());
        listeners.environmentPrepared(environment);
        if (!this.webEnvironment) {
            environment = new EnvironmentConverter(getClassLoader())
                    .convertToStandardEnvironmentIfNecessary(environment);
        }
        return environment;
    }
獲取或創建環境getOrCreateEnvironment

方法名就很直觀,有就直接獲取,沒有就新建

    private ConfigurableEnvironment getOrCreateEnvironment() {
        if (this.environment != null) {
            return this.environment;
        }
        if (this.webApplicationType == WebApplicationType.SERVLET) {
            return new StandardServletEnvironment();
        }
        return new StandardEnvironment();
    }

上篇文章中說過了,咱們是Servlet環境,所以當前方法是返回一個StandardServletEnvironment對象,這個對象的構造過程中調用了customizePropertySources方法(它父類的父類調用的)

    protected void customizePropertySources(MutablePropertySources propertySources) {
        propertySources.addLast(new StubPropertySource("servletConfigInitParams"));
        propertySources.addLast(new StubPropertySource("servletContextInitParams"));
        if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
            propertySources.addLast(new JndiPropertySource("jndiProperties"));
        }
        super.customizePropertySources(propertySources);
    }
  //這是它父類的
 protected void customizePropertySources(MutablePropertySources propertySources) {
        propertySources.addLast(new MapPropertySource("systemProperties", getSystemProperties()));
        propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", getSystemEnvironment()));
    }

可以看出StandardServletEnvironmentpropertySources中添加了兩個StubPropertySource對象,而它的父類添加了一個包含java系統屬性和一個操作系統環境變數的對象

配置 configureEnvironment

protected void configureEnvironment(ConfigurableEnvironment environment,
    String[] args) {
    // 配置PropertySources
    configurePropertySources(environment, args);
    // 配置Profiles
    configureProfiles(environment, args);
}

分別看一下兩個方法

配置PropertySources
protected void configurePropertySources(ConfigurableEnvironment environment,
        String[] args) {
    MutablePropertySources sources = environment.getPropertySources();
    if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
        // 存在預設配置將其放到最後位置
        sources.addLast(
                new MapPropertySource("defaultProperties", this.defaultProperties));
    }
    // 如果存在命令行參數則將原有的替換掉
    if (this.addCommandLineProperties && args.length > 0) {
        String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
        if (sources.contains(name)) {
            PropertySource<?> source = sources.get(name);
            CompositePropertySource composite = new CompositePropertySource(name);
            composite.addPropertySource(new SimpleCommandLinePropertySource(
                    "springApplicationCommandLineArgs", args));
            composite.addPropertySource(source);
            sources.replace(name, composite);
        }
        else {
            // 將其放到第一位置
            sources.addFirst(new SimpleCommandLinePropertySource(args));
        }
    }
}

這裡就體現出了這個命令行參數比應用配置文件的優先順序高的情況了

配置Profiles

從PropertySources中查找spring.profiles.active屬性,存在則將其值添加activeProfiles集合中

protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
    environment.getActiveProfiles(); 
    Set<String> profiles = new LinkedHashSet<>(this.additionalProfiles);
    profiles.addAll(Arrays.asList(environment.getActiveProfiles()));
    environment.setActiveProfiles(StringUtils.toStringArray(profiles));
}
發佈EnvirongmentPreparedEvent事件
綁定環境
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
        try {
            Binder.get(environment).bind("spring.main", Bindable.ofInstance(this));
        }
        catch (Exception ex) {
            throw new IllegalStateException("Cannot bind to SpringApplication", ex);
        }
    }
轉換環境

如果web環境變更為NONE則將StandardServletEnvironment轉換為StandardEnvironment

ConfigurationPropertySources.attach(environment)
public static void attach(Environment environment) {
        Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
        MutablePropertySources sources = ((ConfigurableEnvironment) environment)
                .getPropertySources();
        PropertySource<?> attached = sources.get("configurationProperties");
        if (attached != null && attached.getSource() != sources) {
            sources.remove("configurationProperties");
            attached = null;
        }
        if (attached == null) {
            sources.addFirst(new ConfigurationPropertySourcesPropertySource(
                    "configurationProperties",
                    new SpringConfigurationPropertySources(sources)));
        }
    }

最終這個sources對象的第一個位置放的是它自己,迴圈引用,這個具體的含義還有待挖掘

1


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 黃燜雞米飯最熱賣的外賣之一,國人都喜歡吃,吃過黃燜雞米飯的應該都知道,除了黃燜雞米飯主體外,還可以添加各種配菜,如土豆、香菇、鵪鶉蛋、青菜等。如果需要你來設計一套黃燜雞米飯結賬系統,你該如何設計呢? 前置條件:主體:黃燜雞米飯 價格:16,配菜:土豆 價格:2、香菇 價格:2、鵪鶉蛋 價格:2、青菜 ...
  • 橋接模式(Bridge): 橋接是用於把抽象化與實現化解耦,使得兩者可以獨立變化。 橋接模式的角色: 1)抽象化角色(Abstraction):它是用於定義抽象介面,通常是抽象類而不是介面,其中定義了一個Implementor(實現介面)類型的對象並可以維護該對象,它與Implementor之間具有 ...
  • 1. 下載安裝包 1 2 3 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi # 2.7安裝包 https://www.python.org/ftp/python/3.6.4/python-3.6.4-amd64.ex ...
  • ​一、生成器 1.定義(generator):一邊迴圈一邊計算下一個元素的機制/演算法 2.滿三個條件 (1)每次調用都能產生出for迴圈需要的下一個元素 (2)如果達到最後一個後,能夠爆出StopIteration異常 (3)可以被next函數調用 3.如何生成一個生成器 (1)直接使用 (2)如果 ...
  • # 1.在伺服器上 tomcat 的 bin目錄下找到並打開 catalina.sh 在文件中搜索: ``` JPDA_ADDRESS= ``` 找一個伺服器上沒有被使用的埠,填入,如50005,保存並退出。 > 如何知道某埠有沒有被占用? > 命令: > ``` > lsof -i:50005 ...
  • idea搭建spring源碼閱讀環境 安裝gradle Github下載Spring源碼 新建學習spring源碼的項目 idea搭建spring源碼閱讀環境 安裝gradle Github下載Spring源碼 新建學習spring源碼的項目 安裝gradle Github下載Spring源碼 新建 ...
  • 2019-09-23-23:48:00 今日所學的內容有: ...
  • 一、預設配置文件 二、指定配置文件 三、使用profile指定配置 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...