news 2026/6/8 17:49:44

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/6/7 4:09:56

增长智能体助力企业智慧转型

增长智能体的出现为企业的智慧转型提供了全新的机遇。它通过整合数据分析、自动化处理和智能决策功能&#xff0c;使得企业能够在复杂多变的市场环境中快速适应。这种技术工具不仅提高了运营效率&#xff0c;还能更好地满足客户需求。例如&#xff0c;企业可以实时监测市场动态…

作者头像 李华
网站建设 2026/6/7 19:25:14

用md-editor-v3快速验证产品创意的3种方法

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 快速生成3个基于md-editor-v3的不同产品原型&#xff1a;1. 技术文档协作平台 2. 个人笔记应用 3. 教育类内容编辑器。每个原型只需实现核心功能流&#xff0c;界面简洁&#xff0c…

作者头像 李华
网站建设 2026/6/7 22:53:55

GC 与内存泄漏:如何通过 GC 日志定位内存泄漏问题?

在Java应用开发中&#xff0c;“内存泄漏”是令开发者谈之色变的难题——它如同程序中的“隐形吸血鬼”&#xff0c;会逐渐吞噬系统内存&#xff0c;最终导致应用卡顿、OOM&#xff08;OutOfMemoryError&#xff09;崩溃。而GC&#xff08;垃圾回收&#xff09;作为JVM管理内存…

作者头像 李华
网站建设 2026/6/8 16:09:54

5分钟掌握DNA三维动画:用Manim实现螺旋结构可视化

5分钟掌握DNA三维动画&#xff1a;用Manim实现螺旋结构可视化 【免费下载链接】manim A community-maintained Python framework for creating mathematical animations. 项目地址: https://gitcode.com/GitHub_Trending/man/manim 想要零基础创建专业级DNA双螺旋动画吗…

作者头像 李华
网站建设 2026/6/4 2:26:52

掌握Mona Sans:革命性可变字体提升网页设计体验

掌握Mona Sans&#xff1a;革命性可变字体提升网页设计体验 【免费下载链接】mona-sans Mona Sans, a variable font from GitHub 项目地址: https://gitcode.com/gh_mirrors/mo/mona-sans 在当今数字化时代&#xff0c;网页设计师和开发者们一直在寻找能够提升用户体验…

作者头像 李华