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

springboot jar是如何启动的

我们先来看一个项目的打完包后的MANIFEST.MF文件:

Manifest‐Version: 1.0
Implementation‐Title: spring‐learn
Implementation‐Version: 0.0.1‐SNAPSHOT
Start‐Class: com.tulingxueyuan.Application
Spring‐Boot‐Classes: BOOT‐INF/classes/
Spring‐Boot‐Lib: BOOT‐INF/lib/
Build‐Jdk‐Spec: 1.8
Spring‐Boot‐Version: 2.1.5.RELEASE
Created‐By: Maven Archiver 3.4.0
Main‐Class: org.springframework.boot.loader.JarLauncher

java -jar执行时会默认寻找Main‐Class对应的类并执行对应的main方法,这里为JarLauncher:

public class JarLauncher extends ExecutableArchiveLauncher {static final String BOOT_INF_CLASSES = "BOOT-INF/classes/";static final String BOOT_INF_LIB = "BOOT-INF/lib/";public JarLauncher() {}protected JarLauncher(Archive archive) {super(archive);}@Overrideprotected boolean isNestedArchive(Archive.Entry entry) {if (entry.isDirectory()) {return entry.getName().equals(BOOT_INF_CLASSES);}return entry.getName().startsWith(BOOT_INF_LIB);}public static void main(String[] args) throws Exception {new JarLauncher().launch(args);}}

当jar的方式启动时,走的是JarLauncher的main方法:

protected void launch(String[] args) throws Exception {JarFile.registerUrlProtocolHandler();ClassLoader classLoader = createClassLoader(getClassPathArchives());launch(args, getMainClass(), classLoader);
}
JarFile#registerUrlProtocolHandler():
private static final String PROTOCOL_HANDLER = "java.protocol.handler.pkgs";
private static final String HANDLERS_PACKAGE = "org.springframework.boot.loader";public static void registerUrlProtocolHandler() {String handlers = System.getProperty(PROTOCOL_HANDLER, "");System.setProperty(PROTOCOL_HANDLER,("".equals(handlers) ? HANDLERS_PACKAGE : handlers + "|" + HANDLERS_PACKAGE));resetCachedUrlHandlers();
}
private static void resetCachedUrlHandlers() {try {URL.setURLStreamHandlerFactory(null);}catch (Error ex) {// Ignore}
}

这里主要注册了java.protocol.handler.pkgs属性,默认为org.springframework.boot.loader

createClassLoader(getClassPathArchives())
protected abstract List<Archive> getClassPathArchives() throws Exception;protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {List<URL> urls = new ArrayList<>(archives.size());for (Archive archive : archives) {urls.add(archive.getUrl());}return createClassLoader(urls.toArray(new URL[0]));
}
protected ClassLoader createClassLoader(URL[] urls) throws Exception {return new LaunchedURLClassLoader(urls, getClass().getClassLoader());
}

archives参数由getClassPathArchives类型传入,默认有2个实现:

protected List<Archive> getClassPathArchives() throws Exception {List<Archive> archives = new ArrayList<>(this.archive.getNestedArchives(this::isNestedArchive));postProcessClassPathArchives(archives);return archives;
}
protected abstract boolean isNestedArchive(Archive.Entry entry);
protected void postProcessClassPathArchives(List<Archive> archives) throws Exception {}//JarLauncher实现
static final String BOOT_INF_CLASSES = "BOOT-INF/classes/";
static final String BOOT_INF_LIB = "BOOT-INF/lib/";
protected boolean isNestedArchive(Archive.Entry entry) {if (entry.isDirectory()) {return entry.getName().equals(BOOT_INF_CLASSES);}return entry.getName().startsWith(BOOT_INF_LIB);
}public boolean isNestedArchive(Archive.Entry entry) {if (entry.isDirectory()) {return entry.getName().equals(WEB_INF_CLASSES);}else {return entry.getName().startsWith(WEB_INF_LIB) || entry.getName().startsWith(WEB_INF_LIB_PROVIDED);}}
protected List<Archive> getClassPathArchives() throws Exception {List<Archive> lib = new ArrayList<>();for (String path : this.paths) {for (Archive archive : getClassPathArchives(path)) {if (archive instanceof ExplodedArchive) {List<Archive> nested = new ArrayList<>(archive.getNestedArchives(new ArchiveEntryFilter()));nested.add(0, archive);lib.addAll(nested);}else {lib.add(archive);}}}addNestedEntries(lib);return lib;
}private List<Archive> getClassPathArchives(String path) throws Exception {String root = cleanupPath(handleUrl(path));List<Archive> lib = new ArrayList<>();File file = new File(root);if (!"/".equals(root)) {if (!isAbsolutePath(root)) {file = new File(this.home, root);}if (file.isDirectory()) {debug("Adding classpath entries from " + file);Archive archive = new ExplodedArchive(file, false);lib.add(archive);}}Archive archive = getArchive(file);if (archive != null) {debug("Adding classpath entries from archive " + archive.getUrl() + root);lib.add(archive);}List<Archive> nestedArchives = getNestedArchives(root);if (nestedArchives != null) {debug("Adding classpath entries from nested " + root);lib.addAll(nestedArchives);}return lib;
}

这里主要分析下launch(args, getMainClass(), classLoader)

这里是mainClass的获取:

protected abstract String getMainClass() throws Exception;//默认用的是这个
protected String getMainClass() throws Exception {Manifest manifest = this.archive.getManifest();String mainClass = null;if (manifest != null) {mainClass = manifest.getMainAttributes().getValue("Start-Class");}if (mainClass == null) {throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this);}return mainClass;
}protected String getMainClass() throws Exception {String mainClass = getProperty(MAIN, "Start-Class");if (mainClass == null) {throw new IllegalStateException("No '" + MAIN + "' or 'Start-Class' specified");}return mainClass;
}

来看launch方法:

protected void launch(String[] args, String mainClass, ClassLoader classLoader) throws Exception {Thread.currentThread().setContextClassLoader(classLoader);createMainMethodRunner(mainClass, args, classLoader).run();
}protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) {return new MainMethodRunner(mainClass, args);
}public void run() throws Exception {Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);mainMethod.invoke(null, new Object[] { this.args });
}

通过反射调用mainClass的main方法,项目由此启动起来。这里注意采用的是自定义的classLoader:LaunchedURLClassLoader

相关文章:

springboot jar是如何启动的

我们先来看一个项目的打完包后的MANIFEST.MF文件&#xff1a; Manifest‐Version: 1.0 Implementation‐Title: spring‐learn Implementation‐Version: 0.0.1‐SNAPSHOT Start‐Class: com.tulingxueyuan.Application Spring‐Boot‐Classes: BOOT‐INF/classes/ Spring‐Bo…...

Android 12系统源码_屏幕设备(二)DisplayAdapter和DisplayDevice的创建

前言 在Android 12系统源码_屏幕设备&#xff08;一&#xff09;DisplayManagerService的启动这篇文章中我们具体分析了DisplayManagerService 的启动流程&#xff0c;本篇文章我们将在这个的基础上具体来分析下设备屏幕适配器的创建过程。 一、注册屏幕适配器 系统是在Disp…...

常用Mysql命令

前言 本文列举了一些常见的mysql操作 正文 一、连接和登录 MySQL 1. 使用命令行登录 MySQL 注意&#xff1a;需要将mysql的bin目录导入到环境变量中 mysql -u 用户名 -p示例&#xff1a; mysql -u root -p执行上述命令后&#xff0c;系统会提示输入密码&#xff0c;输入…...

IDEA Debug工具

一、Debug工具栏 自定义debug工具栏&#xff1a;先把debug程序运行起来->右击->配置 常用的工具&#xff1a; 二、DeBug常用图标详解 三、DeBug实践操作 常规Debug&#xff1a;略。 Stream Chain&#xff1a;处理流式语句 Reset Frame&#xff1a;重置方法入栈 …...

ARM64的汇编资源

最近在写一本ARM64的教材&#xff0c;所以在晚上查找了一下相关资源&#xff0c;都是免费开源的&#xff0c;不包括盗版书籍。 Exploring AArch64 assembler Roger Ferrer Ibez的博客文章&#xff0c;写在2016-2017年&#xff0c;内容简单充实&#xff0c;适合入门。 《ARM6…...

实验室安全分级分类管理系统在高校中的具体应用

盛元广通高校实验室安全分级分类管理系统的构建&#xff0c;旨在通过科学合理的管理手段&#xff0c;提高实验室的安全水平&#xff0c;保障师生的人身安全&#xff0c;防止实验事故的发生。这一系统通常包括实验室安全等级评估、分类管理、风险控制、安全教育与培训、应急响应…...

使用 prerenderRoutes 进行预渲染路由

title: 使用 prerenderRoutes 进行预渲染路由 date: 2024/8/20 updated: 2024/8/20 author: cmdragon excerpt: prerenderRoutes 函数是 Nuxt 3 中一个强大的工具,它能够帮助开发者优化页面加载速度和改善用户体验。通过使用 prerenderRoutes,你能够灵活地指定需要预渲染的…...

【深度解析】WRF-LES与PALM微尺度气象大涡模拟

查看原文>>>【深度解析】WRF-LES与PALM微尺度气象大涡模拟 针对微尺度气象的复杂性&#xff0c;大涡模拟&#xff08;LES&#xff09;提供了一种无可比拟的解决方案。微尺度气象学涉及对小范围内的大气过程进行精确模拟&#xff0c;这些过程往往与天气模式、地形影响和…...

redis事件机制

redis服务器是一个由事件驱动(死循环)的程序&#xff0c;它总共就干两件事&#xff1a; 文件事件&#xff1a;利用I/O复用机制&#xff0c;监听Socket等文件描述符发生的事件&#xff0c;如网络请求时间事件&#xff1a;定时触发的事件&#xff0c;负责完成redis内部定时任务&…...

【C++】模拟实现vector

可以把vector看作升级版的数组&#xff0c;可采用下标进行访问&#xff0c;非常高效&#xff0c;大小可动态改变&#xff0c;会自动扩容&#xff0c;数据存储在堆空间上。 VECROR 成员变量、函数及模板总览构造函数和析构函数无参构造函数构造n个元素大小的空间并初始化通过某个…...

【CAN-IDPS】汽车网关信息安全要求以及实验方法

《汽车网关信息安全技术要求及试验方法》是中国的一项国家标准,编号为GB/T 40857-2021,于2021年10月11日发布,并从2022年5月1日起开始实施 。这项标准由全国汽车标准化技术委员会(TC114)归口,智能网联汽车分会(TC114SC34)执行,主管部门为工业和信息化部。 该标准主要…...

EASE-Grid是啥东西?

EASE-Grid&#xff08;Equal-Area Scalable Earth Grid&#xff0c;等面积可扩展地球网格&#xff09;是NASA设计的网格系统&#xff0c;主要用于存储和处理全球范围内的地球科学数据。可以被理解为一种特殊的投影方式&#xff0c;使得在全球范围内进行数据分析和可视化时&…...

前端用户管理模块方法及api分析

用户管理 方法及对应api 搜索 searchSysUser / GetSysUserListByPage 重置 resetData 添加用户 addShow &#xff1a;点击按钮后出现对话框&#xff0c;含有提交 submit / SaveSysUser、取消按钮 修改 editSysUser / UpdateSysUser 删除 deleteById / DeleteSysUser 分配角色…...

microsoft edge怎么关闭安全搜索

microsoft edge浏览器为用户提供了安全搜索功能&#xff0c;旨在帮助用户过滤掉搜索结果中出现的不当信息。然而&#xff0c;有些用户可能觉得安全搜索功能限制了他们的浏览体验或工作需求。下面就给大家带来关闭microsoft edge安全搜索的相关内容&#xff0c;一起来看看吧。&a…...

Qt | QSQLite内存数据库增删改查

点击上方"蓝字"关注我们 01、演示 参数随便设置 查询 修改 右键菜单是重点 手动提交,点击Submit All...

【论文阅读】SegNeXt:重新思考卷积注意力设计

《SegNeXt: Rethinking Convolutional Attention Design for Semantic Segmentation》 原文&#xff1a;https://github.com/Visual-Attention-Network/SegNeXt/blob/main/resources/paper.pdf 源码&#xff1a;https://github.com/Visual-Attention-Network/SegNeXt 1、简介 …...

【C++】String类:标准库介绍

目录 一.预备知识 1.auto关键字 2.范围for 3.迭代器 二.标准库里的string 1.string类的基本介绍 2.构造函数 ​编辑 3.访问及遍历操作 3.1 operator [] 3.2 基于范围for 3.3 使用迭代器 4.迭代器 5.容量操作 5.1 size和length 5.2 capacity 5.3 reserve和resiz…...

MS523非接触式读卡器 IC

MS523 是一款应用于 13.56MHz 非接触式通信中的高集成 度读写卡芯片&#xff0c;它集成了在 13.56MHz 下所有类型的被动非接 触式通信方式和协议&#xff0c;支持 ISO14443A/B 的多层应用。 主要特点  高度集成的解调和解码模拟电路  采用少量外部器件&#…...

仓颉编程语言入门 -- Socket 编程与HTTP 编程概述

仓颉的 Socket 编程概述 在网络通信的广阔天地中&#xff0c;仓颉的Socket编程如同一座桥梁&#xff0c;连接着不同的计算设备&#xff0c;实现了基于传输层协议的数据传输。无论是追求稳定可靠的TCP&#xff0c;还是偏好轻量级、无连接的UDP&#xff0c;Socket都扮演着不可或…...

Oracle基本SQL操作-用户角色权限管理

一、用户权限管理 -- 创建锁定用户&#xff0c;此时用户不可用 create USER zhucl IDENTIFIED BY 123456 account lock; 会提示用户被锁定&#xff1a; -- 删除用户 drop user zhucl;-- 重新创建用户&#xff0c;不锁定 create user zhucl IDENTIFIED BY 123456 account unlo…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...

React hook之useRef

React useRef 详解 useRef 是 React 提供的一个 Hook&#xff0c;用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途&#xff0c;下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器

——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的​​一体化测试平台​​&#xff0c;覆盖应用全生命周期测试需求&#xff0c;主要提供五大核心能力&#xff1a; ​​测试类型​​​​检测目标​​​​关键指标​​功能体验基…...

postgresql|数据库|只读用户的创建和删除(备忘)

CREATE USER read_only WITH PASSWORD 密码 -- 连接到xxx数据库 \c xxx -- 授予对xxx数据库的只读权限 GRANT CONNECT ON DATABASE xxx TO read_only; GRANT USAGE ON SCHEMA public TO read_only; GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only; GRANT EXECUTE O…...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

【HarmonyOS 5 开发速记】如何获取用户信息(头像/昵称/手机号)

1.获取 authorizationCode&#xff1a; 2.利用 authorizationCode 获取 accessToken&#xff1a;文档中心 3.获取手机&#xff1a;文档中心 4.获取昵称头像&#xff1a;文档中心 首先创建 request 若要获取手机号&#xff0c;scope必填 phone&#xff0c;permissions 必填 …...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...

uniapp 开发ios, xcode 提交app store connect 和 testflight内测

uniapp 中配置 配置manifest 文档&#xff1a;manifest.json 应用配置 | uni-app官网 hbuilderx中本地打包 下载IOS最新SDK 开发环境 | uni小程序SDK hbulderx 版本号&#xff1a;4.66 对应的sdk版本 4.66 两者必须一致 本地打包的资源导入到SDK 导入资源 | uni小程序SDK …...

Python Einops库:深度学习中的张量操作革命

Einops&#xff08;爱因斯坦操作库&#xff09;就像给张量操作戴上了一副"语义眼镜"——让你用人类能理解的方式告诉计算机如何操作多维数组。这个基于爱因斯坦求和约定的库&#xff0c;用类似自然语言的表达式替代了晦涩的API调用&#xff0c;彻底改变了深度学习工程…...

API网关Kong的鉴权与限流:高并发场景下的核心实践

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 引言 在微服务架构中&#xff0c;API网关承担着流量调度、安全防护和协议转换的核心职责。作为云原生时代的代表性网关&#xff0c;Kong凭借其插件化架构…...