当前位置: 首页 > news >正文

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); } }
http://www.cnnetsun.cn/news/15208.html

相关文章:

  • 1小时打造Mac专属SSH工具:快马平台实战
  • PIKE-RAG知识库本地化部署之分块
  • DREAM3D完整指南:从入门到精通的材料科学数据分析解决方案
  • 靠谱的自动供包环线分拣机生产厂家
  • 5分钟用VSCode在Ubuntu上搭建Web应用原型
  • 24小时挑战:用AI快速打造‘旺仔‘风格IP原型
  • 零基础搞定Umi项目自动化部署:从代码到上线的完整指南
  • 数学分析简明教程——6.2
  • SSM物业缴费管理系统u8mx4(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
  • 如何在PowerPoint中轻松插入LaTeX公式:终极解决方案
  • Ultralytics YOLOv11终极性能优化:从配置到实战的完整指南
  • 突破传统:3大实战方法让GLM语言模型成为你的AI生产力工具
  • 3步快速解决HeyGem.ai性能问题:终极优化指南
  • 自助项目全解析:适配老板画像业态选择指南
  • 传统链表OUT了!侵入式链表让Nginx、TCMalloc 性能飞跃的秘密武器
  • MinIO效率革命:传统存储方案对比实测
  • AI如何帮你彻底理解box-sizing的奥秘
  • 如何用AI自动生成OpenRGB灯光控制脚本
  • 告别深夜改Bug!CodeGenie帮你快速“驯服”鸿蒙编译错误!
  • 企业IT运维:批量处理设备启动故障(代码10)实战
  • 3天掌握VAR模型:零基础搭建GPT式图像生成系统
  • Headless Recorder完整指南:从零掌握浏览器自动化脚本生成
  • 终极指南:如何用ConvNeXt实现高效语义分割(UperNet完整教程)
  • 包装设计创意大比拼,谁才是行业王者?
  • 项目分享|Tabby:打造你自己的智能代码补全服务
  • 终极音频解锁指南:3分钟掌握浏览器端音乐格式转换
  • Word中批量给手机号打码,分享2种高效加密方法!
  • 5大核心优势解析:为什么Screenbox成为Windows平台最佳免费播放器
  • 【必学收藏】RAG技术详解:解决大模型幻觉的终极指南,从入门到实战
  • 有序数组的平方——双指针