news 2026/9/24 0:18: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/9/23 14:50:31

1小时打造Mac专属SSH工具:快马平台实战

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 快速开发一个基础Mac SSH客户端原型&#xff0c;要求&#xff1a;1. 支持基本的SSH连接功能&#xff1b;2. 保存常用服务器配置&#xff1b;3. 简单的命令历史记录&#xff1b;4. 原…

作者头像 李华
网站建设 2026/9/23 6:14:28

PIKE-RAG知识库本地化部署之分块

最近正在做一个本地RAG项目,即数据需要留在本地,模型也需要本地搭建,特此记录。本系列总体以PIKE-RAG开源知识库为基础,包含本地化改造、FastAPI封装接口,页面搭建等内容。本篇只包含PIKE-RAG开源知识库部署与如何利用本地部署大模型作为对话模型对内容进行分块。 PIKE-R…

作者头像 李华
网站建设 2026/9/23 6:32:21

DREAM3D完整指南:从入门到精通的材料科学数据分析解决方案

DREAM3D完整指南&#xff1a;从入门到精通的材料科学数据分析解决方案 【免费下载链接】DREAM3D Data Analysis program and framework for materials science data analytics, based on the managing framework SIMPL framework. 项目地址: https://gitcode.com/gh_mirrors/…

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

靠谱的自动供包环线分拣机生产厂家

在当今高度竞争的物流与电商行业中&#xff0c;自动供包环线分拣机作为提升分拣效率和准确率的关键设备&#xff0c;其重要性不言而喻。为了帮助用户在众多品牌中做出明智的选择&#xff0c;我们基于市场主流评测数据和技术白皮书&#xff0c;对市场上最具代表性的几款自动供包…

作者头像 李华
网站建设 2026/9/23 5:00:51

5分钟用VSCode在Ubuntu上搭建Web应用原型

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个Ubuntu下基于VSCode的快速Web应用原型生成器。功能&#xff1a;1. 支持React/Vue前端 2. Node.js/Flask后端可选 3. MongoDB/PostgreSQL数据库集成 4. 自动生成CRUD接口 5.…

作者头像 李华
网站建设 2026/9/22 22:23:53

24小时挑战:用AI快速打造‘旺仔‘风格IP原型

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个快速原型工具&#xff0c;能在24小时内完成以下流程&#xff1a;1)根据关键词生成旺仔风格角色概念&#xff1b;2)自动生成三视图和表情集&#xff1b;3)创建基础动画循环&…

作者头像 李华