news 2026/7/1 19:36:27

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/7/2 3:52:57

ER-Save-Editor安全多方计算:游戏存档协作编辑的安全新范式

你是否曾在与朋友分享《艾尔登法环》存档时&#xff0c;担心角色数据被意外篡改&#xff1f;或者在多人协作修改装备属性时&#xff0c;遭遇过存档损坏的困扰&#xff1f;ER-Save-Editor的安全多方计算技术为游戏存档协作提供了全新的安全保障&#xff0c;让存档分享既安全又高…

作者头像 李华
网站建设 2026/7/2 5:16:07

AI智能识别硬件,一键生成万能网卡驱动解决方案

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个基于AI的万能网卡驱动生成工具&#xff0c;要求&#xff1a;1. 自动检测用户网卡硬件信息&#xff08;型号、厂商等&#xff09;2. 通过AI模型匹配最适合的驱动程序 3. 支持…

作者头像 李华
网站建设 2026/7/2 3:04:11

分布式系统中的垃圾回收:分布式缓存的内存清理策略

在分布式系统的架构版图中&#xff0c;缓存是提升性能的“关键引擎”——它将热点数据锚定在内存中&#xff0c;大幅缩短数据访问路径&#xff0c;避免后端数据库或存储系统陷入高频读写的瓶颈。然而&#xff0c;内存资源的稀缺性与分布式环境的复杂性&#xff0c;共同催生了一…

作者头像 李华
网站建设 2026/7/2 3:09:44

《张江科技评论》刊文详解 AVATA+文昌链如何助力中国合规Web 3.0分布式商业模式创新

专业科技评论类杂志《张江科技评论》曾发表文章《区块链技术赋能 Web3.0 分布式商业模式创新》&#xff0c;详细介绍了区块链技术如何为 Web3.0 分布式商业模式夯实数字底座&#xff0c;以及边界智能自主研发的多链跨链分布式应用服务平台 AVATA、开放联盟链文昌链等核心产品如…

作者头像 李华
网站建设 2026/7/2 3:05:08

AntdUI WinForm UI界面库:从零开始的终极安装指南

AntdUI WinForm UI界面库&#xff1a;从零开始的终极安装指南 【免费下载链接】AntdUI &#x1f45a; 基于 Ant Design 设计语言的 Winform 界面库 项目地址: https://gitcode.com/AntdUI/AntdUI 想要为传统的WinForm应用程序注入现代化的设计语言吗&#xff1f;AntdUI正…

作者头像 李华