SpringBoot 之啟動流程詳解
SpringBoot啟動過程簡介
SpringBoot應用程序的啟動過程可以分為以下幾個步驟:
- 加載應用程序上下文
- 掃描應用程序中的所有組件
- 自動配置應用程序環(huán)境
- 啟動嵌入式Web服務器
在下面的章節(jié)中,我們將逐一分析這些步驟的源代碼。
加載應用程序上下文
SpringBoot 應用程序的上下文是一個包含所有應用程序組件的容器。在啟動過程中,SpringBoot 會加載并初始化這個容器。
這個步驟的源代碼在SpringApplication
類中。具體來說,SpringApplication
類的run
方法是這個過程的入口點。在這個方法中,Spring Boot會通過調(diào)用createApplicationContext
方法來創(chuà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); }
在這個方法中,SpringBoot 會根據(jù)應用程序類型(Servlet或Reactive)選擇合適的上下文類。然后,它會使用 Java 反射機制來實例化這個類并返回一個可配置的應用程序上下文對象。
掃描應用程序中的所有組件
在上一步中,SpringBoot創(chuàng)建了應用程序上下文。在這一步中,SpringBoot會掃描應用程序中的所有組件并將它們注冊到應用程序上下文中。
這個步驟的源代碼在SpringApplication
類中的scan
方法中。具體來說,在這個方法中,SpringBoot 會創(chuàng)建一個SpringBootBeanDefinitionScanner
對象,并使用它來掃描應用程序中的所有組件。
下面是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); } }
在這個方法中,SpringBoot 會創(chuàng)建一個ClassPathScanningCandidateComponentProvider
對象,并使用它來掃描應用程序中的所有組件。這個對象會掃描指定包路徑下的所有類,并將它們轉(zhuǎn)換為 Spring 的 Bean 定義。這些 Bean 定義將被注冊到應用程序上下文中。
自動配置應用程序環(huán)境
在上一步中,SpringBoot將應用程序中的所有組件注冊到應用程序上下文中。在這一步中,SpringBoot將自動配置應用程序環(huán)境,包括配置數(shù)據(jù)源、事務管理器、JPA等。
這個步驟的源代碼在SpringApplication
類中的configureEnvironment
方法中。在這個方法中,Spring Boot會創(chuàng)建一個SpringApplicationRunListeners
對象,并使用它來配置應用程序環(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); }
在這個方法中,SpringBoot 會創(chuàng)建一個ApplicationArguments
對象,并將其轉(zhuǎn)換為一個命令行屬性源。然后,它會調(diào)用listeners
中的environmentPrepared
方法來通知應用程序環(huán)境已經(jīng)準備好了。隨后,SpringBoot 會綁定屬性源到應用程序環(huán)境中,并調(diào)用listeners
中的environmentPrepared
方法來通知應用程序環(huán)境已經(jīng)準備好了。
啟動嵌入式Web服務器
在上一步中,SpringBoot 將應用程序環(huán)境自動配置完成。在這一步中,SpringBoot 將啟動嵌入式Web服務器,以便應用程序能夠提供 Web 服務。
這個步驟的源代碼在SpringApplication
類中的run
方法中。具體來說,在這個方法中,SpringBoot 會根據(jù)應用程序類型(Servlet或Reactive)選擇合適的嵌入式Web服務器,并使用它來啟動應用程序。
下面是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; }
在這個方法中,SpringBoot 會使用一個StopWatch
對象來計算應用程序啟動時間。然后,它會調(diào)用listeners
中的starting
方法來通知應用程序即將啟動。接著,SpringBoot 會準備應用程序環(huán)境,并使用它來創(chuàng)建應用程序上下文。隨后,SpringBoot 會調(diào)用listeners
中的started
方法來通知應用程序已經(jīng)啟動。最后,SpringBoot 會調(diào)用callRunners
方法來運行所有的CommandLineRunner
和ApplicationRunner
組件。
總結(jié)
在本文中,我們深入分析了 SpringBoot 應用程序的啟動過程的源代碼。我們了解了 SpringBoot 如何加載應用程序上下文、掃描應用程序中的所有組件、自動配置應用程序環(huán)境以及啟動嵌入式Web服務器。這些步驟的源代碼展示了 SpringBoot 如何簡化應用程序的開發(fā)和部署。
以上就是SpringBoot 之啟動流程詳解的詳細內(nèi)容,更多關(guān)于SpringBoot啟動流程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot服務設(shè)置禁止server.point端口的使用
本文主要介紹了SpringBoot服務設(shè)置禁止server.point端口的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-01-01idea 隱藏target,iml等不需要展示的文件(推薦)
這篇文章主要介紹了idea 隱藏target,iml等不需要展示的文件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Maven是什么?Maven的概念+作用+倉庫的介紹+常用命令的詳解
Maven是一個項目管理工具,它包含了一個對象模型。一組標準集合,一個依賴管理系統(tǒng)。和用來運行定義在生命周期階段中插件目標和邏輯.,本文給大家介紹Maven的概念+作用+倉庫的介紹+常用命令,感興趣的的朋友跟隨小編一起看看吧2020-09-09