【分布式微服务】SpringBoot启动原理

news2024/6/30 0:05:15

一、SpringBoot启动类

@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
public class PracticeApplication {
    public static void main(String[] args) {
        SpringApplication.run(PracticeApplication.class, args);
    }
}

二、源码解析


	/**
	 * Static helper that can be used to run a {@link SpringApplication} from the
	 * specified sources using default settings and user supplied arguments.
	 * @param primarySources the primary sources to load
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return the running {@link ApplicationContext}
	 */
	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		return new SpringApplication(primarySources).run(args);
	}

整体流程如下:
在这里插入图片描述

2-1 创建SpringApplication对象


	/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details. The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #SpringApplication(ResourceLoader, Class...)
	 * @see #setSources(Set)
	 */
	public SpringApplication(Class<?>... primarySources) {
		this(null, primarySources);
	}

	/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details. The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param resourceLoader the resource loader to use
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #setSources(Set)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		// 将启动类放入primarySources
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		// 获取应用的类型:servlet或者是webFlux
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		// 从spring.factories 中去获取所有key:org.springframework.context.ApplicationContextInitializer
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		// 去spring.factories 中去获取所有key: org.springframework.context.ApplicationListener
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		// 从Application找到main方法所在的那个类
		this.mainApplicationClass = deduceMainApplicationClass();
	}

基本流程:
1.获取启动类;
2.获取web应用类型;
3.从spring.factories中读取对外扩展的ApplicationContextInitializer ,ApplicationListener;
4.根据main方法找出所在类。

2-2 SpringApplication.run()


	/**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return a running {@link ApplicationContext}
	 */
	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
		// 1.获取所有的SpringApplicationRunListeners,调用它们的starting()方法,通知它们当前SpringBoot应用即将启动
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
		    // 2.配置当前应用将要使用的Environment,遍历调用所有SpringApplicationRunListener的environmentPrepared()的方法,通知它们当前SpringBoot应用使用的Environment准备好了
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
			// 3.如果SpringApplication的showBanner属性被设置为true,则打印banner(横幅)
			Banner printedBanner = printBanner(environment);
			// 4.创建ApplicationContext上下文对象(利用反射)
			// 例如:servlet项目:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2a32fb6
			context = createApplicationContext();
			// 5. 遍历调用所有SpringApplicationRunListener的contextPrepared()方法
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			// 6.调用ApplicationContext的refresh()方法,将所有的BeanDefinition转化为Bean对象放置于IOC容器中
			refreshContext(context);
			// protected方法,用户可去实现自己的逻辑
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) { // 默认为true
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
			// 7.调用SpringApplicationRunListeners的started()方法,将所有的监听器进行开启
			listeners.started(context);
			// 8.查找当前ApplicationContext中是否注册有ApplicationRunner或者CommandLineRunner,如果有,则遍历执行它们的run方法
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
		    // 出现异常后,调用listeners.failed()、context.close()方法,并打印出异常信息
			handleRunFailure(context, ex, listeners);
			throw new IllegalStateException(ex);
		}

		try {
		    // 9.遍历执行SpringApplicationRunListener的running()方法
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

基本流程:
1.从spring.factories 读取 listener,进行预启动;
2.设置环境变量、banner信息、配置信息等;
3. 创建springApplication上下文;
4. 预初始化上下文;
5.调用refresh 加载ioc容器(加载所有的自动配置类、创建servlet容器); -》底层是Spring框架来实现的
6.监听器启动。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/186072.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

云原生安全方案介绍

方案介绍 安全狗云原生应用安全解决方案(CNAPP, Cloud-Native Application Protection Platform)采用主机安全Agent和安全容器相结合的技术&#xff0c;既落地了“安全左移”的概念&#xff0c;又能对云原生容器做全面保护&#xff0c;同时能灵活地跟容器编排体系相结合&#x…

map find/count源码分析

参考博客&#xff1a;c map find方法源码解析 众所周知&#xff0c;如果一个数据结构想作为map的key&#xff0c;则必须重载 operator < 否则编译将会报错。但是operator 则是不需要的。那么当查找某个key是否存在的时候&#xff0c;map内部是怎么实现的呢&#xff1f; 结…

1301738-40-8,Bis-maleimides-amine,双马来酰亚胺-氨基

一、基础产品数据&#xff08;Basic Product Data&#xff09;&#xff1a;CAS号&#xff1a;1301738-40-8中文名&#xff1a;三臂双马来酰亚胺-氨基&#xff0c;双马来酰亚胺-氨基&#xff0c;活性交联剂&#xff0c;异官能团三臂交联剂英文名&#xff1a;Bis-Maleimides amin…

【C语言进阶】柔性数组

目录一&#xff1a;柔性数组的特点二&#xff1a;柔性数组的使用三&#xff1a;模拟实现柔性数组在C99中&#xff0c;结构中的最后一个元素允许是未知大小的数组&#xff0c;这就叫做柔性数组成员 以下是柔性数组的两种写法&#xff1a; //写法一&#xff1a; struct S {int n…

Python笔记 · 鸭子类型 / Duck Typing

1. 问题的由来 我初次意识到鸭子类型在存在是在学习Sklearn时&#xff0c;在《Hands-On Machine Learing》一书的第二章&#xff0c;作者提供了一个自定义的Tansformer&#xff0c;使用自定义Transformer的好处在于&#xff1a;你既可以实现自己需要的数据处理逻辑&#xff0c…

【应用】SpringCloud -- Gateway

SpringCloud -- GatewayGateway 网关概述Gateway 的功能Gateway 核心概念Gateway 网关搭建Gateway 的配置及使用predicate 断言filter 过滤器GlobalFilter 全局过滤器Gateway 的几点问题过滤器执行顺序跨域问题Gateway 网关概述 Gateway 的功能 在微服务当中&#xff0c;有很…

PG15 pg_basebackup 代码解析

背景 先前 PG 版本 pg_basebackup 的代码较为复杂&#xff0c;pg_basebackup 在备份过程中做了很多事情&#xff0c;但这部分代码逻辑没有完全解耦&#xff0c;导致一个文件里包含了很多功能的逻辑&#xff0c;影响了代码的可读性和可修改性。 因此&#xff0c;PG 15 针对这部…

模电学习3. 差模、共模干扰与安规电容

模电学习3. 差模、共模干扰与安规电容一、简介1. 安规2. 电源安规标准二、差模干扰与共模干扰1. 共模干扰2. 差模干扰&#xff08;1&#xff09;定义&#xff08;2&#xff09;来源与特性3. 测量4. 消除电源线路中的差模干扰&#xff08;1&#xff09;差模电容5. 消除电源线路中…

TOOM加强网络舆情监控制定处置预案,抓好舆情监控管理?

舆情预案是指根据对未来舆情发展趋势的预测&#xff0c;制定预先准备的舆情处置方案&#xff0c;以应对可能出现的舆情危机。舆情预案包括舆情预警体系、舆情应对策略、应对措施等内容&#xff0c;旨在在舆情危机发生前进行有效的预防和准备&#xff0c;避免舆情危机扩大&#…

95. BERT预训练数据代码

为了预训练之前实现的BERT模型&#xff0c;我们需要以理想的格式生成数据集&#xff0c;以便于两个预训练任务&#xff1a;遮蔽语言模型和下一句预测。一方面&#xff0c;最初的BERT模型是在两个庞大的图书语料库和英语维基百科的合集上预训练的&#xff0c;但它很难吸引这本书…

Zookeeper 教程

Zookeeper 教程Zookeeper 概述分布式应用Zookeeper 架构图ZooKeeper当中的主从与主备&#xff1a;Zookeeper的特性分布式应用的优点分布式应用的挑战什么是Apache ZooKeeper&#xff1f;ZooKeeper的好处Zookeeper 基础ZooKeeper的架构层次命名空间Znode的类型Sessions&#xff…

九龙证券|外资放大招,400亿巨头狂飙!这只翻倍股却突然崩了

昨日超级“开门红”之后&#xff0c;今天上午港股又有多只重磅个股大幅涨超10%。 今日上午&#xff0c;港股整体窄幅震荡&#xff0c;上午收盘&#xff0c;恒生指数微跌0.05%&#xff0c;恒生科技指数涨0.22%。 职业板块方面&#xff0c;媒体、软件服务、电信等涨幅居前&#…

【BLE】ANCS(Apple Notification Center Service)

目录1. 前言1.1 名词解释1.2 ANCS概述2. ANCS的特征2.1 通知源2.2 控制点和数据源2.3 获取通知属性2.4 获取应用属性2.5 执行通知操作2.6 通知操作3. 更多详情参考1. 前言 1.1 名词解释 NP(Notification Provider)&#xff1a;消息提供者&#xff0c;指的是ANCS服务的生产者&…

【JavaSE】入门概述(1~41)

1.Java视频及配套资料下载指南 2.Java基础学习导读 Java语言的三个层面 基本语法&#xff1a;变量、运算符、流程控制、数组面向对象&#xff1a;把数据及对数据的操作方法放在一起&#xff0c;作为一个相互依存的整体——对象高级应用&#xff1a;多线程、集合、IO流、网络…

性能怪兽-Nginx常用配置指北

目录 安装 Nginx操作命令 https反向代理 负载均衡 轮询 加权轮询 最少连接 加权最少连接 IP Hash 普通 Hash 动静分离 资源压缩 缓冲区 缓存机制 解决跨域 防盗链设计 配置SLL证书 性能优化 打开长连接配置 开启零拷贝技术 开启无延迟或多包共发机制 调整W…

Python连接Liunx中mysql数据库-增删改查

上一篇文章已经讲解了如何连接liunx中的mysql数据库&#xff0c;如果没有连接好数据库的话&#xff0c;可以看这一篇文章 增删改查Mysql中查询操作1.创建游标2.定义一个sql的查询语句3.调用游标内的sql语句执行操作4.打印出查询结果5.完整代码6.指定查询Mysql中新增操作1.单条数…

jupyter 常用记录

安装windows环境下 运行cmd 然后&#xff1a;在命令提示窗口输入pip install jupyter,然后回车&#xff1b;完成后运行 jupyter notebook 前言 提起jupyter notebook&#xff0c;应该很多学习过Python的同学都不陌生。虽然用jupyter notebook的同学相对较少&#xff0c;但是提…

Java多级缓存是为了解决什么的?

前言 提到缓存&#xff0c;想必每一位软件工程师都不陌生&#xff0c;它是目前架构设计中提高性能最直接的方式。 缓存技术存在于应用场景的方方面面。从网站提高性能的角度分析&#xff0c;缓存可以放在浏览器&#xff0c;可以放在反向代理服务器&#xff0c;还可以放在应用…

使用JINJA2模板部署自定义文件(RH294)

在ansible中有许多模板可以用于修改现有的文件比如—— lineinfile和blockinfile但是&#xff01;我们还有一种更加便捷而且牛皮的方法为其构建模板在部署该文件是自动为受管主机自定义次模板配置文件而这些模板中非常有名的就是 JINJA2ansible将Jinja2模板系统用于模板文件 并…

Java设计模式-职责链模式Chain of Responsibility

介绍 职责链模式&#xff08;Chain of Responsibility Pattern&#xff09;, 又叫 责任链模式&#xff0c;为请求创建了一个接收者对象的链(简单示意图)。这种模式对请求的发送者和接收者进行解耦。职责链模式通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理…