SpringBoot啟動流程分析1:SpringApplication類初始化過程

目錄

一、SpringApplication初始化過程

1.1、SpringBoot項目的main函數(shù)
1.2、 SpringApplication() 構(gòu)造方法

  • 1.2.1、deduceWebApplicationType();
  • 1.2.2、 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
  • 1.2.3、 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

二、總結(jié)

一、SpringApplication初始化過程

1.1、SpringBoot項目的main函數(shù)

常規(guī)的這個主類如下圖所示,我們一般會這樣去寫。

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

在這個類中需要關(guān)注的是

  • @SpringBootApplication
  • SpringApplication.run()

關(guān)于 @SpringBootApplication 注解,在后面分析SpringBoot自動裝配的章節(jié)會展開去分析。

本章節(jié)中我們需要關(guān)注的就是 SpringApplication.run() 方法。

查看run()方法的實現(xiàn),如下面代碼所示,我們發(fā)現(xiàn)其實其首先是創(chuàng)建了 SpringApplication 的實例,然后調(diào)用了 SpringApplication 的run()方法,那本章我們關(guān)注的就是 SpringApplication 創(chuàng)建實例的過程。

/**
     * Static helper that can be used to run a {@link SpringApplication} from the
     * specified sources using default settings and user supplied arguments.
     *
     * @param primarySources the primary sources to load
     * @param args           the application arguments (usually passed from a Java main method)
     * @return the running {@link ApplicationContext}
     */
    public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        return run(new Class[]{primarySource}, args);
    }

1.2、 SpringApplication() 構(gòu)造方法

    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.lazyInitialization = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

1.2.1、deduceWebApplicationType();

該方法推斷應用的類型。 SERVLET REACTIVE NONE

    private static final String[] SERVLET_INDICATOR_CLASSES = new String[]{"javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext"};
    private static final String WEBMVC_INDICATOR_CLASS = "org.springframework.web.servlet.DispatcherServlet";
    private static final String WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
    private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";
    private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
    private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext";

    private WebApplicationType() {
    }

    static WebApplicationType deduceFromClasspath() {
        if (ClassUtils.isPresent("org.springframework.web.reactive.DispatcherHandler", (ClassLoader)null) && !ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", (ClassLoader)null) && !ClassUtils.isPresent("org.glassfish.jersey.servlet.ServletContainer", (ClassLoader)null)) {
            return REACTIVE;
        } else {
            String[] var0 = SERVLET_INDICATOR_CLASSES;
            int var1 = var0.length;

            for(int var2 = 0; var2 < var1; ++var2) {
                String className = var0[var2];
                if (!ClassUtils.isPresent(className, (ClassLoader)null)) {
                    return NONE;
                }
            }

            return SERVLET;
        }
    }

返回類型是WebApplicationType的枚舉類型, WebApplicationType 有三個枚舉,三個枚舉的解釋如其中注釋

具體的判斷邏輯如下:

  • WebApplicationType.REACTIVE classpath下存在org.springframework.web.reactive.DispatcherHandler

  • WebApplicationType.SERVLET classpath下存在javax.servlet.Servlet或者org.springframework.web.context.ConfigurableWebApplicationContext

  • WebApplicationType.NONE 不滿足以上條件。

1.2.2、 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));

初始化classpath下 META-INF/spring.factories中已配置的ApplicationContextInitializer。

/**
  * 通過指定的classloader 從META-INF/spring.factories獲取指定的Spring的工廠實例
  * @param type
  * @param parameterTypes
  * @param args
  * @param <T>
  * @return
  */
    private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
        ClassLoader classLoader = this.getClassLoader();
        // Use names and ensure unique to protect against duplicates
        //通過指定的classLoader從 META-INF/spring.factories 的資源文件中,
        //讀取 key 為 type.getName() 的 value
        Set<String> names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
        List<T> instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
        AnnotationAwareOrderComparator.sort(instances);
        return instances;
    }

看看 getSpringFactoriesInstances 都干了什么,看源碼,有一個方法很重要 loadFactoryNames() 這個方法很重要,這個方法是spring-core中提供的從META-INF/spring.factories中獲取指定的類(key)的同一入口方法。

在這里,獲取的是key為 org.springframework.context.ApplicationContextInitializer 的類。

debug看看都獲取到了哪些

image

上面說了,是從classpath下 META-INF/spring.factories中獲取,我們驗證一下:

image
image

發(fā)現(xiàn)在上圖所示的兩個工程中找到了debug中看到的6條結(jié)果。 ApplicationContextInitializer 是Spring框架的類, 這個類的主要目的就是在 ConfigurableApplicationContext 調(diào)用refresh()方法之前,回調(diào)這個類的initialize方法。通過 ConfigurableApplicationContext 的實例獲取容器的環(huán)境Environment,從而實現(xiàn)對配置文件的修改完善等工作。

關(guān)于怎么實現(xiàn)自定義的 ApplicationContextInitializer 請看我的另一篇專門介紹該類的博客。

1.2.3、 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

初始化classpath下 META-INF/spring.factories中已配置的 ApplicationListener。

ApplicationListener 的加載過程和上面的 ApplicationContextInitializer 類的加載過程是一樣的。不多說了,至于 ApplicationListener 是spring的事件監(jiān)聽器,典型的觀察者模式,通過 ApplicationEvent 類和 ApplicationListener 接口,可以實現(xiàn)對spring容器全生命周期的監(jiān)聽,當然也可以自定義監(jiān)聽事件。為了梳理springboot的啟動流程在這里先不說這個了。后面有時間的話再介紹。

關(guān)于ApplicationContextInitializer的詳細介紹請看<SpringBoot之ApplicationContextInitializer的理解和使用>

二、總結(jié)

關(guān)于 SpringApplication 類的構(gòu)造過程,到這里我們就梳理完了??v觀 SpringApplication 類的實例化過程,我們可以看到,合理的利用該類,我們能在spring容器創(chuàng)建之前做一些預備工作,和定制化的需求。

比如,自定義SpringBoot的Banner,比如自定義事件監(jiān)聽器,再比如在容器refresh之前通過自定義 ApplicationContextInitializer 修改配置一些配置或者獲取指定的bean都是可以的。。。

下一節(jié)開始分析SpringBoot容器的構(gòu)建過程,也就是那個大家多少都看過的run();方法。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容