news 2026/4/16 13:54:45

Springboot启动流程(源代码解读):

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Springboot启动流程(源代码解读):

一:核心代码:

package com.spring; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; // 核心注解 @SpringBootApplication public class Application{ public static void main(String[] args) { // 启动springboot ConfigurableApplicationContext run = SpringApplication.run(Application.class, args); } }

SpringApplication.run(Application.class, args);内部最终走到这个方法

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args); }

二:new SpringApplication(primarySources):

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 = Collections.emptySet(); this.isCustomEnvironment = false; this.lazyInitialization = false; this.applicationContextFactory = ApplicationContextFactory.DEFAULT; this.applicationStartup = ApplicationStartup.DEFAULT; this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); // 确定应用程序类型 this.webApplicationType = WebApplicationType.deduceFromClasspath(); this.bootstrapRegistryInitializers = this.getBootstrapRegistryInitializersFromSpringFactories(); // 加载初始化器this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 加载监听器 this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); // 设置程序运行的主类 this.mainApplicationClass = this.deduceMainApplicationClass(); }

三:.run(args):

public ConfigurableApplicationContext run(String... args) { // 实例化计时器 StopWatch stopWatch = new StopWatch(); // 开始计时 stopWatch.start(); // 获取上下文 DefaultBootstrapContext bootstrapContext = this.createBootstrapContext(); ConfigurableApplicationContext context = null; this.configureHeadlessProperty(); // 创建所有 Spring 运行监听器并发布应用启动事件 SpringApplicationRunListeners listeners = this.getRunListeners(args); // 通过上下文启动监听器 listeners.starting(bootstrapContext, this.mainApplicationClass); try { // 设置应用程序参数 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 准备环境变量 ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments); // 将 spring.beaninfo.ignore 的默认值值设为true,意思是跳过beanInfo的搜索 this.configureIgnoreBeanInfo(environment); // 打印banner Banner printedBanner = this.printBanner(environment); // 创建应用程序上下文 context = this.createApplicationContext(); context.setApplicationStartup(this.applicationStartup); // 准备上下文环境 this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); // 刷新上下文 this.refreshContext(context); // 启动后的一些处理,留给用户扩展使用,目前这个方法里面是空的 this.afterRefresh(context, applicationArguments); // 结束计时器 stopWatch.stop(); if (this.logStartupInfo) { (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch); } // 发布上下文准备就绪事件 listeners.started(context); this.callRunners(context, applicationArguments); } catch (Throwable var10) { this.handleRunFailure(context, var10, listeners); throw new IllegalStateException(var10); } try { listeners.running(context); return context; } catch (Throwable var9) { this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null); throw new IllegalStateException(var9); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/15 18:36:31

SQL优化:比解决多行返回更重要的3个设计原则

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个SQL设计评估工具&#xff0c;能够&#xff1a;1) 检查数据库schema设计是否容易导致多行子查询问题 2) 分析查询模式给出优化建议 3) 提供三种替代方案&#xff1a;JOIN重写…

作者头像 李华
网站建设 2026/4/15 21:58:03

如何用AI快速解决Node.js数字信封初始化错误

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个Node.js错误诊断工具&#xff0c;专门用于分析和解决error:03000086:digital envelope routines::initialization error。工具应能自动检测Node.js版本、OpenSSL配置和环境…

作者头像 李华
网站建设 2026/4/16 10:12:32

企业IT运维实战:用万能网卡驱动批量部署500+办公电脑

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个企业级网卡驱动批量部署工具&#xff0c;功能包括&#xff1a;1. 支持AD域环境下的静默安装 2. 多网卡型号自动识别和适配 3. 部署状态监控和报告生成 4. 驱动版本管理和回…

作者头像 李华
网站建设 2026/4/15 21:28:25

AI为数字媒资“把关”:意识形态审核平台的技术通俗解读

当下数字媒资呈爆炸式增长&#xff0c;短视频、直播、图文等内容每秒都在海量产生。这些内容里&#xff0c;意识形态导向直接影响信息传播的价值走向&#xff0c;人工审核不仅效率低、易漏判&#xff0c;还难以应对海量内容的冲击。数字媒资AI意识形态审核平台&#xff0c;就是…

作者头像 李华
网站建设 2026/4/16 13:04:51

排烟风机公司排行榜Top1!排烟风机哪个品牌好?

在“双碳”目标与智慧城市建设双重驱动下&#xff0c;厂房通风领域正经历从“功能满足”到“高效、节能”的深刻变革。英飞排烟风机凭借全品类产品矩阵、极端工况适应性&#xff0c;为地铁隧道、工业厂房、商业综合体、商用建筑、展览中心、机场、写字楼、宾馆、饭店、影剧院、…

作者头像 李华
网站建设 2026/4/15 21:26:40

SonarLint实战:如何在大规模项目中提升代码质量

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个实战案例展示工具&#xff0c;模拟大规模项目中使用SonarLint进行代码审查的过程。包括代码扫描、问题分类&#xff08;如Bug、漏洞、代码异味&#xff09;、修复建议生成、…

作者头像 李华