news 2026/4/18 10:05:31

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/18 7:56:06

LIO-SAM高精度激光惯性里程计:从理论到工程实践

LIO-SAM高精度激光惯性里程计&#xff1a;从理论到工程实践 【免费下载链接】LIO-SAM LIO-SAM: Tightly-coupled Lidar Inertial Odometry via Smoothing and Mapping 项目地址: https://gitcode.com/GitHub_Trending/li/LIO-SAM LIO-SAM是一个基于紧耦合激光雷达惯性里…

作者头像 李华
网站建设 2026/4/18 8:37:35

Hermes-4 14B:混合推理与低拒绝率重构企业级AI应用范式

Hermes-4 14B&#xff1a;混合推理与低拒绝率重构企业级AI应用范式 【免费下载链接】Hermes-4-14B 项目地址: https://ai.gitcode.com/hf_mirrors/NousResearch/Hermes-4-14B 导语&#xff1a;当AI学会"按需思考"——开源大模型的企业级突破 企业在部署AI助…

作者头像 李华
网站建设 2026/4/18 1:40:37

零基础入门:如何使用Cline编程助手学习编程

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个新手学习平台&#xff0c;功能包括&#xff1a;1. 提供基础编程概念的AI讲解&#xff08;如变量、循环、函数&#xff09;&#xff1b;2. 根据用户学习进度生成练习代码&am…

作者头像 李华
网站建设 2026/4/18 7:38:27

如何快速创建专业神经网络图:NN-SVG完全使用手册

如何快速创建专业神经网络图&#xff1a;NN-SVG完全使用手册 【免费下载链接】NN-SVG NN-SVG: 是一个工具&#xff0c;用于创建神经网络架构的图形表示&#xff0c;可以参数化地生成图形&#xff0c;并将其导出为SVG文件。 项目地址: https://gitcode.com/gh_mirrors/nn/NN-S…

作者头像 李华
网站建设 2026/4/18 8:56:13

3.5%成本颠覆千亿模型格局:Cogito v2 70B混合推理技术革新

3.5%成本颠覆千亿模型格局&#xff1a;Cogito v2 70B混合推理技术革新 【免费下载链接】cogito-v2-preview-llama-70B 项目地址: https://ai.gitcode.com/hf_mirrors/unsloth/cogito-v2-preview-llama-70B 导语 当AI不再"过度依赖计算资源"——旧金山AI初创…

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

电商系统MySQL分表实战:订单数据拆分方案

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 设计一个电商订单分表系统&#xff0c;基于订单创建时间按月拆分数据。需要生成&#xff1a;1)分表创建的SQL脚本 2)数据迁移的存储过程 3)跨分表查询的视图。要求处理1000万订单数…

作者头像 李华