摘要:

  • 利用IDEA等工具打包会出现springboot-0.0.1-SNAPSHOT.jar,springboot-0.0.1-SNAPSHOT.jar.original,前面说过它们之间的关系了,接下来我们就一探究竟,它们之间到底有什么联系。

文件对比:

  • 进入target目录,unzip springboot-0.0.1-SNAPSHOT.jar -d jar命令将springboot-0.0.1-SNAPSHOT.jar解压到jar目录

  • 进入target目录,unzip springboot-0.0.1-SNAPSHOT.jar.original -d original命令将springboot-0.0.1-SNAPSHOT.jar.original解压到original目录

前面文章分析过springboot-0.0.1-SNAPSHOT.jar.original不能执行,将它进行repackage后生成springboot-0.0.1-SNAPSHOT.jar就成了我们的可执行fat jar,对比上面文件会发现可执行 fat jar和original jar目录不一样,最关键的地方是多了org.springframework.boot.loader这个包,这个就是我们平时java -jar springboot-0.0.1-SNAPSHOT.jar命令启动的奥妙所在。MANIFEST.MF文件里面的内容包含了很多关键的信息

  1. Manifest-Version: 1.0
  2. Start-Class: com.github.dqqzj.springboot.SpringbootApplication
  3. Spring-Boot-Classes: BOOT-INF/classes/
  4. Spring-Boot-Lib: BOOT-INF/lib/
  5. Build-Jdk-Spec: 1.8
  6. Spring-Boot-Version: 2.1.6.RELEASE
  7. Created-By: Maven Archiver 3.4.0
  8. Main-Class: org.springframework.boot.loader.JarLauncher

相信不用多说大家都能明白Main-Class: org.springframework.boot.loader.JarLauncher是我们 java -jar命令启动的入口,后续会进行分析,Start-Class: com.github.dqqzj.springboot.SpringbootApplication才是我们程序的入口主函数。

Springboot jar启动源码分析

  1. public class JarLauncher extends ExecutableArchiveLauncher {
  2. static final String BOOT_INF_CLASSES = "BOOT-INF/classes/";
  3. static final String BOOT_INF_LIB = "BOOT-INF/lib/";
  4. public JarLauncher() {
  5. }
  6. protected JarLauncher(Archive archive) {
  7. super(archive);
  8. }
  9. /**
  10. * 判断是否归档文件还是文件系统的目录 可以猜想基于文件系统一样是可以启动的
  11. */
  12. protected boolean isNestedArchive(Entry entry) {
  13. return entry.isDirectory() ? entry.getName().equals("BOOT-INF/classes/") : entry.getName().startsWith("BOOT-INF/lib/");
  14. }
  15. public static void main(String[] args) throws Exception {
  16. /**
  17. * 进入父类初始化构造器ExecutableArchiveLauncher
  18. * launch方法交给Launcher执行
  19. */
  20. (new JarLauncher()).launch(args);
  21. }
  22. }
  23. public abstract class ExecutableArchiveLauncher extends Launcher {
  24. private final Archive archive;
  25. public ExecutableArchiveLauncher() {
  26. try {
  27. /**
  28. * 使用父类Launcher加载资源,包括BOOT-INF的classes和lib下面的所有归档文件
  29. */
  30. this.archive = this.createArchive();
  31. } catch (Exception var2) {
  32. throw new IllegalStateException(var2);
  33. }
  34. }
  35. protected ExecutableArchiveLauncher(Archive archive) {
  36. this.archive = archive;
  37. }
  38. protected final Archive getArchive() {
  39. return this.archive;
  40. }
  41. /**
  42. * 从归档文件中获取我们的应用程序主函数
  43. */
  44. protected String getMainClass() throws Exception {
  45. Manifest manifest = this.archive.getManifest();
  46. String mainClass = null;
  47. if (manifest != null) {
  48. mainClass = manifest.getMainAttributes().getValue("Start-Class");
  49. }
  50. if (mainClass == null) {
  51. throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this);
  52. } else {
  53. return mainClass;
  54. }
  55. }
  56. protected List<Archive> getClassPathArchives() throws Exception {
  57. List<Archive> archives = new ArrayList(this.archive.getNestedArchives(this::isNestedArchive));
  58. this.postProcessClassPathArchives(archives);
  59. return archives;
  60. }
  61. protected abstract boolean isNestedArchive(Entry entry);
  62. protected void postProcessClassPathArchives(List<Archive> archives) throws Exception {
  63. }
  64. }
  65. public abstract class Launcher {
  66. public Launcher() {
  67. }
  68. protected void launch(String[] args) throws Exception {
  69. /**
  70. *注册协议处理器,由于Springboot是 jar in jar 所以要重写jar协议才能读取归档文件
  71. */
  72. JarFile.registerUrlProtocolHandler();
  73. ClassLoader classLoader = this.createClassLoader(this.getClassPathArchives());
  74. /**
  75. * this.getMainClass()交给子类ExecutableArchiveLauncher实现
  76. */
  77. this.launch(args, this.getMainClass(), classLoader);
  78. }
  79. protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
  80. List<URL> urls = new ArrayList(archives.size());
  81. Iterator var3 = archives.iterator();
  82. while(var3.hasNext()) {
  83. Archive archive = (Archive)var3.next();
  84. urls.add(archive.getUrl());
  85. }
  86. return this.createClassLoader((URL[])urls.toArray(new URL[0]));
  87. }
  88. /**
  89. * 该类加载器是fat jar的关键的一处,因为传统的类加载器无法读取jar in jar模型,所以springboot进行了自己实现
  90. */
  91. protected ClassLoader createClassLoader(URL[] urls) throws Exception {
  92. return new LaunchedURLClassLoader(urls, this.getClass().getClassLoader());
  93. }
  94. protected void launch(String[] args, String mainClass, ClassLoader classLoader) throws Exception {
  95. Thread.currentThread().setContextClassLoader(classLoader);
  96. this.createMainMethodRunner(mainClass, args, classLoader).run();
  97. }
  98. /**
  99. * 创建应用程序主函数运行器
  100. */
  101. protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) {
  102. return new MainMethodRunner(mainClass, args);
  103. }
  104. protected abstract String getMainClass() throws Exception;
  105. protected abstract List<Archive> getClassPathArchives() throws Exception;
  106. /**
  107. * 得到我们的启动jar的归档文件
  108. */
  109. protected final Archive createArchive() throws Exception {
  110. ProtectionDomain protectionDomain = this.getClass().getProtectionDomain();
  111. CodeSource codeSource = protectionDomain.getCodeSource();
  112. URI location = codeSource != null ? codeSource.getLocation().toURI() : null;
  113. String path = location != null ? location.getSchemeSpecificPart() : null;
  114. if (path == null) {
  115. throw new IllegalStateException("Unable to determine code source archive");
  116. } else {
  117. File root = new File(path);
  118. if (!root.exists()) {
  119. throw new IllegalStateException("Unable to determine code source archive from " + root);
  120. } else {
  121. return (Archive)(root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
  122. }
  123. }
  124. }
  125. }
  126. public class MainMethodRunner {
  127. private final String mainClassName;
  128. private final String[] args;
  129. public MainMethodRunner(String mainClass, String[] args) {
  130. this.mainClassName = mainClass;
  131. this.args = args != null ? (String[])args.clone() : null;
  132. }
  133. /**
  134. * 最终执行的方法,可以发现是利用的反射调用的我们应用程序的主函数
  135. */
  136. public void run() throws Exception {
  137. Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);
  138. Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
  139. mainMethod.invoke((Object)null, this.args);
  140. }
  141. }

小结:

内容太多了,未涉及归档文件,协议处理器,打包war同样的可以用命令启动等,感兴趣的读者请亲自去调试一番,添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-loader</artifactId>
  4. </dependency>

IDEA进行启动类的配置

Springboot源码分析之jar探秘的更多相关文章

  1. Springboot源码分析之项目结构

    Springboot源码分析之项目结构 摘要: 无论是从IDEA还是其他的SDS开发工具亦或是https://start.spring.io/ 进行解压,我们都会得到同样的一个pom.xml文件 4. ...

  2. SpringBoot源码分析之SpringBoot的启动过程

    SpringBoot源码分析之SpringBoot的启动过程 发表于 2017-04-30   |   分类于 springboot  |   0 Comments  |   阅读次数 SpringB ...

  3. 从SpringBoot源码分析 配置文件的加载原理和优先级

    本文从SpringBoot源码分析 配置文件的加载原理和配置文件的优先级     跟入源码之前,先提一个问题:   SpringBoot 既可以加载指定目录下的配置文件获取配置项,也可以通过启动参数( ...

  4. SpringBoot源码分析(二)启动原理

    Springboot的jar启动方式,是通过IOC容器启动 带动了Web容器的启动 而Springboot的war启动方式,是通过Web容器(如Tomcat)的启动 带动了IOC容器相关的启动 一.不 ...

  5. springboot源码分析-SpringApplication

    SpringApplication SpringApplication类提供了一种方便的方法来引导从main()方法启动的Spring应用程序 SpringBoot 包扫描注解源码分析 @Spring ...

  6. Springboot源码分析之代理三板斧

    摘要: 在Spring的版本变迁过程中,注解发生了很多的变化,然而代理的设计也发生了微妙的变化,从Spring1.x的ProxyFactoryBean的硬编码岛Spring2.x的Aspectj注解, ...

  7. Springboot源码分析之事务拦截和管理

    摘要: 在springboot的自动装配事务里面,InfrastructureAdvisorAutoProxyCreator ,TransactionInterceptor,PlatformTrans ...

  8. Springboot源码分析之Spring循环依赖揭秘

    摘要: 若你是一个有经验的程序员,那你在开发中必然碰到过这种现象:事务不生效.或许刚说到这,有的小伙伴就会大惊失色了.Spring不是解决了循环依赖问题吗,它是怎么又会发生循环依赖的呢?,接下来就让我 ...

  9. 从SpringBoot源码分析 主程序配置类加载过程

    1.@Import(AutoConfigurationPackages.Registrar.class) 初始SpringBoot 我们知道在SpringBoot 启动类上有一个@SpringBoot ...

随机推荐

  1. EnjoyingSoft之Mule ESB开发教程第四篇:Mule Expression Language - MEL表达式

    目录 1. MEL的优势 2. MEL的使用场景 3. MEL的示例 4. MEL的上下文对象 5. MEL的Variable 6. MEL访问属性 7. MEL操作符 本篇主要介绍Mule表达式语言 ...

  2. CF1194D 1-2-K Game (博弈论)

    CF1194D 1-2-K Game 一道简单的博弈论题 首先让我们考虑没有k的情况: 1. (n mod 3 =0) 因为n可以被分解成若干个3相加 而每个3可以被分解为1+2或2+1 所以无论A出 ...

  3. Guid几种格式及之间的互换,以及利用Base64缩短guid的长度到22个字符和还原

    1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid().ToSt ...

  4. [leetcode] 464. Can I Win (Medium)

    原题链接 两个人依次从1~maxNum中选取数字(不可重复选取同一个),累和.当一方选取数字累和后结果大于等于给定的目标数字,则此人胜利. 题目给一个maxNum和targetNum,要求判断先手能否 ...

  5. Codeforces 1144 E. Median String

    原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...

  6. ubuntu root用户 默认密码

    ubuntu安装好后,root初始密码(默认密码)不知道,需要设置. 1.先用安装时候的用户登录进入系统 2.输入:sudo passwd  按回车 3.输入新密码,重复输入密码,最后提示passwd ...

  7. Java EE编程思想

    组件--容器 编程思想 组件:由程序员根据特定的业务需求编程实现. 容器:组件的运行环境,为组件提供必须的底层基础功能. 组件通过调用容器提供的标准服务来与外界交互,容器提供的标准服务有命名服务.数据 ...

  8. Soso(嗖嗖)移动 java 项目

    1.接口 通话服务 package Soso; // 接口 通话服务 public interface CallService { public abstract int call(int minCo ...

  9. centos7主机间免密登录、复制文件

    下面实例为三个节点间 1.分别在三个节点设置域名映射 vi /etc/hosts  在文件末尾追加 192.168.10.121 node1 192.168.10.122 node2 192.168. ...

  10. windows上使用pip下载东西时报编码错误问题解决方法

    原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码.解决办法是: python目录 Python27\Lib\site-packages 建一个文件site ...