news 2026/7/16 16:30:49

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/15 3:00:51

1小时快速原型:用JTAG实现设备诊断工具

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 快速开发一个JTAG设备诊断工具原型&#xff0c;要求&#xff1a;1.自动检测JTAG链设备 2.读取关键寄存器值 3.基本故障诊断 4.简洁的GUI界面 5.支持结果导出。使用PythonQT框架&…

作者头像 李华
网站建设 2026/7/14 10:20:01

Mac用户必备:5个高效SSH工具对比与实战技巧

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个SSH工具对比网页&#xff0c;要求&#xff1a;1. 对比Termius、iTerm2、Royal TSX、SecureCRT和原生终端&#xff1b;2. 包含功能对比表格&#xff1b;3. 每种工具的特色功…

作者头像 李华
网站建设 2026/7/16 6:20:37

如何用AI快速生成JTAG调试工具代码

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 请生成一个基于Python的JTAG调试工具代码框架&#xff0c;要求包含以下功能&#xff1a;1.支持基本的TAP控制器状态机实现 2.提供简单的JTAG指令发送接口 3.包含TCK/TMS/TDI/TDO信号…

作者头像 李华
网站建设 2026/7/13 19:32:58

Vue3 defineOptions实战:构建可配置表单组件

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个可配置的表单组件FormBuilder&#xff0c;使用defineOptions定义&#xff1a;1. props接收fields数组配置表单字段&#xff1b;2. emits提交事件&#xff1b;3. 计算属性动…

作者头像 李华
网站建设 2026/7/15 11:48:08

ReadCat免费开源小说阅读器:5分钟快速上手完整指南

ReadCat免费开源小说阅读器&#xff1a;5分钟快速上手完整指南 【免费下载链接】read-cat 一款免费、开源、简洁、纯净、无广告的小说阅读器 项目地址: https://gitcode.com/gh_mirrors/re/read-cat ReadCat是一款完全免费、开源纯净的小说阅读器&#xff0c;致力于为用…

作者头像 李华
网站建设 2026/7/15 16:28:33

无停顿 GC 实现原理:ZGC 如何做到毫秒级停顿?

在 Java 应用的性能优化领域&#xff0c;垃圾回收&#xff08;GC&#xff09;停顿始终是绕不开的“老大难”问题。对于金融交易、实时数据分析、高频交易等核心业务&#xff0c;即使是数百毫秒的停顿都可能引发系统超时、交易失败等严重后果。传统 GC 如 CMS、G1 虽在不断优化&…

作者头像 李华