溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

SpringBoot啟動(dòng)流程是什么

發(fā)布時(shí)間:2023-05-05 15:37:49 來(lái)源:億速云 閱讀:130 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“SpringBoot啟動(dòng)流程是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“SpringBoot啟動(dòng)流程是什么”吧!

SpringBoot啟動(dòng)過(guò)程簡(jiǎn)介

SpringBoot應(yīng)用程序的啟動(dòng)過(guò)程可以分為以下幾個(gè)步驟:

  • 加載應(yīng)用程序上下文

  • 掃描應(yīng)用程序中的所有組件

  • 自動(dòng)配置應(yīng)用程序環(huán)境

  • 啟動(dòng)嵌入式Web服務(wù)器

加載應(yīng)用程序上下文

SpringBoot 應(yīng)用程序的上下文是一個(gè)包含所有應(yīng)用程序組件的容器。在啟動(dòng)過(guò)程中,SpringBoot 會(huì)加載并初始化這個(gè)容器。

這個(gè)步驟的源代碼在SpringApplication類(lèi)中。具體來(lái)說(shuō),SpringApplication類(lèi)的run方法是這個(gè)過(guò)程的入口點(diǎn)。在這個(gè)方法中,Spring Boot會(huì)通過(guò)調(diào)用createApplicationContext方法來(lái)創(chuàng)建應(yīng)用程序上下文。

下面是createApplicationContext方法的源代碼:

protected ConfigurableApplicationContext createApplicationContext() {
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {
            switch (this.webApplicationType) {
                case SERVLET:
                    contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
                    break;
                case REACTIVE:
                    contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                    break;
                default:
                    contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
            }
        }
        catch (ClassNotFoundException ex) {
            throw new IllegalStateException(
                    "Unable to create a default ApplicationContext, " +
                    "please specify an ApplicationContextClass", ex);
        }
    }
    return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

在這個(gè)方法中,SpringBoot 會(huì)根據(jù)應(yīng)用程序類(lèi)型(Servlet或Reactive)選擇合適的上下文類(lèi)。然后,它會(huì)使用 Java 反射機(jī)制來(lái)實(shí)例化這個(gè)類(lèi)并返回一個(gè)可配置的應(yīng)用程序上下文對(duì)象。

掃描應(yīng)用程序中的所有組件

在上一步中,SpringBoot創(chuàng)建了應(yīng)用程序上下文。在這一步中,SpringBoot會(huì)掃描應(yīng)用程序中的所有組件并將它們注冊(cè)到應(yīng)用程序上下文中。

這個(gè)步驟的源代碼在SpringApplication類(lèi)中的scan方法中。具體來(lái)說(shuō),在這個(gè)方法中,SpringBoot 會(huì)創(chuàng)建一個(gè)SpringBootBeanDefinitionScanner對(duì)象,并使用它來(lái)掃描應(yīng)用程序中的所有組件。

下面是scan方法的源代碼:

private void scan(String... basePackages) {
    if (ObjectUtils.isEmpty(basePackages)) {
        return;
    }
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            this.includeFilters, this.excludeFilters, this.resourceLoader);
    scanner.setResourceLoader(this.resourceLoader);
    scanner.setEnvironment(this.environment);
    scanner.setIncludeAnnotationConfig(this.useAnnotatedConfig);
    scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
        @Override
        protected boolean matchClassName(String className) {
            return getExcludeClassNames().contains(className);
        }
    });
    for (String basePackage : basePackages) {
        scanner.findCandidateComponents(basePackage).forEach(this.componentDefinitions::add);
    }
}

在這個(gè)方法中,SpringBoot 會(huì)創(chuàng)建一個(gè)ClassPathScanningCandidateComponentProvider對(duì)象,并使用它來(lái)掃描應(yīng)用程序中的所有組件。這個(gè)對(duì)象會(huì)掃描指定包路徑下的所有類(lèi),并將它們轉(zhuǎn)換為 Spring 的 Bean 定義。這些 Bean 定義將被注冊(cè)到應(yīng)用程序上下文中。

自動(dòng)配置應(yīng)用程序環(huán)境

在上一步中,SpringBoot將應(yīng)用程序中的所有組件注冊(cè)到應(yīng)用程序上下文中。在這一步中,SpringBoot將自動(dòng)配置應(yīng)用程序環(huán)境,包括配置數(shù)據(jù)源、事務(wù)管理器、JPA等。

這個(gè)步驟的源代碼在SpringApplication類(lèi)中的configureEnvironment方法中。在這個(gè)方法中,Spring Boot會(huì)創(chuàng)建一個(gè)SpringApplicationRunListeners對(duì)象,并使用它來(lái)配置應(yīng)用程序環(huán)境。

下面是configureEnvironment方法的源代碼:

private void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
    if (this.addCommandLineProperties) {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        environment.getPropertySources().addLast(new CommandLinePropertySource(applicationArguments));
    }
    this.listeners.environmentPrepared(environment);
    if (this.logStartupInfo) {
        this.logStartupInfo(environment);
    }
    ConfigurationPropertySources.attach(environment);
    Binder.get(environment).bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(this.sources));
    if (!this.isCustomEnvironment) {
        EnvironmentConverter.configureEnvironment(environment, this.deduceEnvironmentClass());
    }
    this.listeners.environmentPrepared(environment);
}

在這個(gè)方法中,SpringBoot 會(huì)創(chuàng)建一個(gè)ApplicationArguments對(duì)象,并將其轉(zhuǎn)換為一個(gè)命令行屬性源。然后,它會(huì)調(diào)用listeners中的environmentPrepared方法來(lái)通知應(yīng)用程序環(huán)境已經(jīng)準(zhǔn)備好了。隨后,SpringBoot 會(huì)綁定屬性源到應(yīng)用程序環(huán)境中,并調(diào)用listeners中的environmentPrepared方法來(lái)通知應(yīng)用程序環(huán)境已經(jīng)準(zhǔn)備好了。

啟動(dòng)嵌入式Web服務(wù)器

在上一步中,SpringBoot 將應(yīng)用程序環(huán)境自動(dòng)配置完成。在這一步中,SpringBoot 將啟動(dòng)嵌入式Web服務(wù)器,以便應(yīng)用程序能夠提供 Web 服務(wù)。

這個(gè)步驟的源代碼在SpringApplication類(lèi)中的run方法中。具體來(lái)說(shuō),在這個(gè)方法中,SpringBoot 會(huì)根據(jù)應(yīng)用程序類(lèi)型(Servlet或Reactive)選擇合適的嵌入式Web服務(wù)器,并使用它來(lái)啟動(dòng)應(yīng)用程序。

下面是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);
        configureIgnoreBeanInfo(environment);
        Banner printedBanner = printBanner(environment);
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(
                SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        refreshContext(context);
        afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }
    try {
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

在這個(gè)方法中,SpringBoot 會(huì)使用一個(gè)StopWatch對(duì)象來(lái)計(jì)算應(yīng)用程序啟動(dòng)時(shí)間。然后,它會(huì)調(diào)用listeners中的starting方法來(lái)通知應(yīng)用程序即將啟動(dòng)。接著,SpringBoot 會(huì)準(zhǔn)備應(yīng)用程序環(huán)境,并使用它來(lái)創(chuàng)建應(yīng)用程序上下文。隨后,SpringBoot 會(huì)調(diào)用listeners中的started方法來(lái)通知應(yīng)用程序已經(jīng)啟動(dòng)。最后,SpringBoot 會(huì)調(diào)用callRunners方法來(lái)運(yùn)行所有的CommandLineRunnerApplicationRunner組件。

到此,相信大家對(duì)“SpringBoot啟動(dòng)流程是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI