我说 Spring Boot 不能用 Jar 包启动?leader 过来就是一 jo。。。

开发 架构
Spring Boot 提供了一个名叫 spring-boot-maven-plugin 的 maven 项目打包插件,可以方便的将 Spring Boot 项目打成 jar 包。这样我们就不再需要部署 Tomcat 、Jetty等之类的 Web 服务器容器啦。

[[407460]]

哈喽,大家好,我是指北君。

可能很多初学者会比较困惑,Spring Boot 是如何做到将应用代码和所有的依赖打包成一个独立的 Jar 包,因为传统的 Java 项目打包成 Jar 包之后,需要通过 -classpath 属性来指定依赖,才能够运行。我们今天就来分析讲解一下 Spring Boot 的启动原理。

1. Spring Boot 打包插件

Spring Boot 提供了一个名叫 spring-boot-maven-plugin 的 maven 项目打包插件,可以方便的将 Spring Boot 项目打成 jar 包。这样我们就不再需要部署 Tomcat 、Jetty等之类的 Web 服务器容器啦。

我们先看一下 Spring Boot 打包后的结构是什么样的,打开 target 目录我们发现有两个jar包:

  • hello-0.0.1-SNAPSHOT.jar:17.3MB
  • hello-0.0.1-SNAPSHOT.jar.original:3KB

其中,hello-0.0.1-SNAPSHOT.jar 是通过 Spring Boot 提供的打包插件采用新的格式打成 Fat Jar,包含了所有的依赖;而 hello-0.0.1-SNAPSHOT.jar.original 则是Java原生的打包方式生成的,仅仅只包含了项目本身的内容。

2. SpringBoot FatJar 的组织结构

我们将 Spring Boot 打的可执行 Jar 展开后的结构如下所示:

  1. ├── BOOT-INF 
  2. │   ├── classes 
  3. │   │   ├── application.properties 
  4. │   │   └── com 
  5. │   │       └── javanorth 
  6. │   │           └── hello 
  7. │   │               └── HelloApplication.class 
  8. │   └── lib 
  9. │       ├── spring-boot-2.5.0.RELEASE.jar 
  10. │       ├── spring-boot-autoconfigure-2.5.0.RELEASE.jar 
  11. │       ├── spring-boot-configuration-processor-2.5.0.RELEASE.jar 
  12. │       ├── spring-boot-starter-2.5.0.RELEASE.jar 
  13. │       ├── ... 
  14. ├── META-INF 
  15. │   ├── MANIFEST.MF 
  16. │   └── maven 
  17. │       └── com.javanorth 
  18. │           └── hello 
  19. │               ├── pom.properties 
  20. │               └── pom.xml 
  21. │    
  22. ├── org 
  23. │   └── springframework 
  24. │       └── boot 
  25. │           └── loader 
  26. │               ├── ExecutableArchiveLauncher.class 
  27. │               ├── JarLauncher.class 
  28. │               ├── Launcher.class 
  29. │               ├── MainMethodRunner.class 
  30. │               ├── ... 
  • BOOT-INF目录:包含了我们的项目代码(classes目录),以及所需要的依赖(lib 目录)
  • META-INF目录:通过 MANIFEST.MF 文件提供 Jar包的元数据,声明了 jar 的启动类
  • org.springframework.boot.loader :Spring Boot 的加载器代码,实现的 Jar in Jar 加载的魔法源

我们看到,如果去掉BOOT-INF目录,这将是一个非常普通且标准的Jar包,包括元信息以及可执行的代码部分,其/META-INF/MAINFEST.MF指定了Jar包的启动元信息,org.springframework.boot.loader 执行对应的逻辑操作。

3. MAINFEST.MF 元信息分析

元信息内容如下所示:

  1. Manifest-Version: 1.0 
  2. Created-By: Maven Jar Plugin 3.2.0 
  3. Build-Jdk-Spec: 11 
  4. Implementation-Title: hello 
  5. Implementation-Version: 0.0.1-SNAPSHOT 
  6. Main-Class: org.springframework.boot.loader.JarLauncher 
  7. Start-Class: com.javanorth.hello.HelloApplication 
  8. Spring-Boot-Version: 2.5.0 
  9. Spring-Boot-Classes: BOOT-INF/classes/ 
  10. Spring-Boot-Lib: BOOT-INF/lib/ 
  11. Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx 
  12. Spring-Boot-Layers-Index: BOOT-INF/layers.idx 

它相当于一个 Properties 配置文件,每一行都是一个配置项目。重点来看看两个配置项:

  • Main-Class 配置项:Java 规定的 jar 包的启动类,这里设置为 spring-boot-loader 项目的 JarLauncher 类,进行 Spring Boot 应用的启动。
  • Start-Class 配置项:Spring Boot 规定的主启动类,这里设置为我们定义的 Application 类。
  • Spring-Boot-Classes 配置项:指定加载应用类的入口
  • Spring-Boot-Lib 配置项: 指定加载应用依赖的库

4. 启动原理

Spring Boot 的启动原理如下图所示:

5. 源码分析

5.1 org.springframework.boot.loader.JarLauncher

JarLauncher 类是针对 Spring Boot jar 包的启动类, 完整的类图如下所示:

Spring Boot Start jar 2

其中的 WarLauncher 类,是针对 Spring Boot war 包的启动类。启动类 org.springframework.boot.loader.JarLauncher 并非为项目中引入类,而是 spring-boot-maven-plugin 插件 repackage 追加进去的。接下来我们先来看一下 JarLauncher 的源码,比较简单,如下图所示:

  1. public class JarLauncher extends ExecutableArchiveLauncher { 
  2.     private static final String DEFAULT_CLASSPATH_INDEX_LOCATION = "BOOT-INF/classpath.idx"
  3.     static final EntryFilter NESTED_ARCHIVE_ENTRY_FILTER = (entry) -> { 
  4.         if (entry.isDirectory()) { 
  5.             return entry.getName().equals("BOOT-INF/classes/"); 
  6.         } 
  7.         return entry.getName().startsWith("BOOT-INF/lib/"); 
  8.     }; 
  9.     public JarLauncher() { 
  10.     } 
  11.     protected JarLauncher(Archive archive) { 
  12.         super(archive); 
  13.     } 
  14.     @Override 
  15.     protected ClassPathIndexFile getClassPathIndex(Archive archive) throws IOException { 
  16.         // Only needed for exploded archives, regular ones already have a defined order 
  17.         if (archive instanceof ExplodedArchive) { 
  18.             String location = getClassPathIndexFileLocation(archive); 
  19.             return ClassPathIndexFile.loadIfPossible(archive.getUrl(), location); 
  20.         } 
  21.         return super.getClassPathIndex(archive); 
  22.     } 
  23.     private String getClassPathIndexFileLocation(Archive archive) throws IOException { 
  24.         Manifest manifest = archive.getManifest(); 
  25.         Attributes attributes = (manifest != null) ? manifest.getMainAttributes() : null
  26.         String location = (attributes != null) ? attributes.getValue(BOOT_CLASSPATH_INDEX_ATTRIBUTE) : null
  27.         return (location != null) ? location : DEFAULT_CLASSPATH_INDEX_LOCATION; 
  28.     } 
  29.     @Override 
  30.     protected boolean isPostProcessingClassPathArchives() { 
  31.         return false
  32.     } 
  33.     @Override 
  34.     protected boolean isSearchCandidate(Archive.Entry entry) { 
  35.         return entry.getName().startsWith("BOOT-INF/"); 
  36.     } 
  37.     @Override 
  38.     protected boolean isNestedArchive(Archive.Entry entry) { 
  39.         return NESTED_ARCHIVE_ENTRY_FILTER.matches(entry); 
  40.     } 
  41.     public static void main(String[] args) throws Exception { 
  42.         new JarLauncher().launch(args); 
  43.     } 

当执行 java -jar 命令或执行解压后的 org.springframework.boot.loader.JarLauncher 类时,JarLauncher 会将 BOOT-INF/classes 下的类文件和 BOOT-INF/lib 下依赖的jar加入到classpath下,后调用 META-INF/MANIFEST.MF 文件 Start-Class 属性 [指向项目中的 com.javanorth.hello.HelloApplicatioin 启动类] 完成应用程序的启动。

JarLauncher 假定依赖项jar包含在 /BOOT-INF/lib 目录中,并且应用程序类包含在 /BOOT-INF/classes 目录中。它的 main 方法调用的则是基类 Launcher 定义的 launch 方法,而 Launcher 是ExecutableArchiveLauncher 的父类。

5.2 org.springframework.boot.loader.ExecutableArchiveLauncher

ExecutableArchiveLauncher 是 JarLauncher 的直接父类,继承了 Launcher 基类,并实现部分抽象方法

  1. public abstract class ExecutableArchiveLauncher extends Launcher { 
  2.     private static final String START_CLASS_ATTRIBUTE = "Start-Class"
  3.     protected static final String BOOT_CLASSPATH_INDEX_ATTRIBUTE = "Spring-Boot-Classpath-Index"
  4.     private final Archive archive; 
  5.     private final ClassPathIndexFile classPathIndex; 
  6.     public ExecutableArchiveLauncher() { 
  7.         try { 
  8.             this.archive = createArchive(); 
  9.             this.classPathIndex = getClassPathIndex(this.archive); 
  10.         } 
  11.         catch (Exception ex) { 
  12.             throw new IllegalStateException(ex); 
  13.         } 
  14.     } 
  15.     protected ExecutableArchiveLauncher(Archive archive) { 
  16.         try { 
  17.             this.archive = archive; 
  18.             this.classPathIndex = getClassPathIndex(this.archive); 
  19.         } 
  20.         catch (Exception ex) { 
  21.             throw new IllegalStateException(ex); 
  22.         } 
  23.     } 
  24.     protected ClassPathIndexFile getClassPathIndex(Archive archive) throws IOException { 
  25.         return null
  26.     } 
  27.     @Override 
  28.     protected String getMainClass() throws Exception { 
  29.         Manifest manifest = this.archive.getManifest(); 
  30.         String mainClass = null
  31.         if (manifest != null) { 
  32.             mainClass = manifest.getMainAttributes().getValue(START_CLASS_ATTRIBUTE); 
  33.         } 
  34.         if (mainClass == null) { 
  35.             throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this); 
  36.         } 
  37.         return mainClass; 
  38.     } 
  39.     @Override 
  40.     protected ClassLoader createClassLoader(Iterator<Archive> archives) throws Exception { 
  41.         List<URL> urls = new ArrayList<>(guessClassPathSize()); 
  42.         while (archives.hasNext()) { 
  43.             urls.add(archives.next().getUrl()); 
  44.         } 
  45.         if (this.classPathIndex != null) { 
  46.             urls.addAll(this.classPathIndex.getUrls()); 
  47.         } 
  48.         return createClassLoader(urls.toArray(new URL[0])); 
  49.     } 
  50.     private int guessClassPathSize() { 
  51.         if (this.classPathIndex != null) { 
  52.             return this.classPathIndex.size() + 10; 
  53.         } 
  54.         return 50; 
  55.     } 
  56.     @Override 
  57.     protected Iterator<Archive> getClassPathArchivesIterator() throws Exception { 
  58.         Archive.EntryFilter searchFilter = this::isSearchCandidate; 
  59.         Iterator<Archive> archives = this.archive.getNestedArchives(searchFilter, 
  60.                 (entry) -> isNestedArchive(entry) && !isEntryIndexed(entry)); 
  61.         if (isPostProcessingClassPathArchives()) { 
  62.             archives = applyClassPathArchivePostProcessing(archives); 
  63.         } 
  64.         return archives; 
  65.     } 
  66.     private boolean isEntryIndexed(Archive.Entry entry) { 
  67.         if (this.classPathIndex != null) { 
  68.             return this.classPathIndex.containsEntry(entry.getName()); 
  69.         } 
  70.         return false
  71.     } 
  72.     private Iterator<Archive> applyClassPathArchivePostProcessing(Iterator<Archive> archives) throws Exception { 
  73.         List<Archive> list = new ArrayList<>(); 
  74.         while (archives.hasNext()) { 
  75.             list.add(archives.next()); 
  76.         } 
  77.         postProcessClassPathArchives(list); 
  78.         return list.iterator(); 
  79.     } 
  80.     protected boolean isSearchCandidate(Archive.Entry entry) { 
  81.         return true
  82.     } 
  83.     protected abstract boolean isNestedArchive(Archive.Entry entry); 
  84.     protected boolean isPostProcessingClassPathArchives() { 
  85.         return true
  86.     } 
  87.     protected void postProcessClassPathArchives(List<Archive> archives) throws Exception { 
  88.     } 
  89.     @Override 
  90.     protected boolean isExploded() { 
  91.         return this.archive.isExploded(); 
  92.     } 
  93.     @Override 
  94.     protected final Archive getArchive() { 
  95.         return this.archive; 
  96.     } 

5.3 org.springframework.boot.loader.Launcher

如下则是 Launcher 的源码

  1. launch 方法会首先创建类加载器,而后判断 jar 是否在 MANIFEST.MF 文件中设置了 jarmode 属性。
  2. 如果没有设置,launchClass 的值就来自 getMainClass() 返回,该方法由子类实现,返回 MANIFEST.MF 中配置的 START_CLASS_ATTRIBUTE 属性值
  3. 调用 createMainMethodRunner 方法,构建一个 MainMethodRunner 对象并调用其 run 方法

jarmode 是创建 docker 镜像时用到的参数,使用该参数是为了生成带有多个 layer 信息的镜像,这里暂不注意

  1. public abstract class Launcher { 
  2.     private static final String JAR_MODE_LAUNCHER = "org.springframework.boot.loader.jarmode.JarModeLauncher"
  3.     protected void launch(String[] args) throws Exception { 
  4.         if (!isExploded()) { 
  5.             JarFile.registerUrlProtocolHandler(); 
  6.         } 
  7.         ClassLoader classLoader = createClassLoader(getClassPathArchivesIterator()); 
  8.         String jarMode = System.getProperty("jarmode"); 
  9.         String launchClass = (jarMode != null && !jarMode.isEmpty()) ? JAR_MODE_LAUNCHER : getMainClass(); 
  10.         launch(args, launchClass, classLoader); 
  11.     } 
  12.     @Deprecated 
  13.     protected ClassLoader createClassLoader(List<Archive> archives) throws Exception { 
  14.         return createClassLoader(archives.iterator()); 
  15.     } 
  16.     protected ClassLoader createClassLoader(Iterator<Archive> archives) throws Exception { 
  17.         List<URL> urls = new ArrayList<>(50); 
  18.         while (archives.hasNext()) { 
  19.             urls.add(archives.next().getUrl()); 
  20.         } 
  21.         return createClassLoader(urls.toArray(new URL[0])); 
  22.     } 
  23.     protected ClassLoader createClassLoader(URL[] urls) throws Exception { 
  24.         return new LaunchedURLClassLoader(isExploded(), getArchive(), urls, getClass().getClassLoader()); 
  25.     } 
  26.     protected void launch(String[] args, String launchClass, ClassLoader classLoader) throws Exception { 
  27.         Thread.currentThread().setContextClassLoader(classLoader); 
  28.         createMainMethodRunner(launchClass, args, classLoader).run(); 
  29.     } 
  30.     protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) { 
  31.         return new MainMethodRunner(mainClass, args); 
  32.     } 
  33.     protected abstract String getMainClass() throws Exception; 
  34.     protected Iterator<Archive> getClassPathArchivesIterator() throws Exception { 
  35.         return getClassPathArchives().iterator(); 
  36.     } 
  37.     @Deprecated 
  38.     protected List<Archive> getClassPathArchives() throws Exception { 
  39.         throw new IllegalStateException("Unexpected call to getClassPathArchives()"); 
  40.     } 
  41.     protected final Archive createArchive() throws Exception { 
  42.         ProtectionDomain protectionDomain = getClass().getProtectionDomain(); 
  43.         CodeSource codeSource = protectionDomain.getCodeSource(); 
  44.         URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null
  45.         String path = (location != null) ? location.getSchemeSpecificPart() : null
  46.         if (path == null) { 
  47.             throw new IllegalStateException("Unable to determine code source archive"); 
  48.         } 
  49.         File root = new File(path); 
  50.         if (!root.exists()) { 
  51.             throw new IllegalStateException("Unable to determine code source archive from " + root); 
  52.         } 
  53.         return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root)); 
  54.     } 
  55.     protected boolean isExploded() { 
  56.         return false
  57.     } 
  58.     protected Archive getArchive() { 
  59.         return null
  60.     } 

5.4 org.springframework.boot.loader.MainMethodRunner

从名字可以判断这是一个目标类main方法的执行器,此时的 mainClassName 被赋值为 MANIFEST.MF 中配置的 START_CLASS_ATTRIBUTE 属性值,也就是 com.javanorth.hello.HelloApplication,之后便是通过反射执行 HelloApplication 的 main 方法,从而达到启动 Spring Boot 的效果。

  1. public class MainMethodRunner { 
  2.     private final String mainClassName; 
  3.     private final String[] args; 
  4.     public MainMethodRunner(String mainClass, String[] args) { 
  5.         this.mainClassName = mainClass; 
  6.         this.args = (args != null) ? args.clone() : null
  7.     } 
  8.     public void run() throws Exception { 
  9.         Class<?> mainClass = Class.forName(this.mainClassName, false, Thread.currentThread().getContextClassLoader()); 
  10.         Method mainMethod = mainClass.getDeclaredMethod("main", String[].class); 
  11.         mainMethod.setAccessible(true); 
  12.         mainMethod.invoke(null, new Object[] { this.args }); 
  13.     } 

总结

jar 包类似于 zip 压缩文件,只不过相比 zip 文件多了一个 META-INF/MANIFEST.MF 文件,该文件在构建 jar 包时自动创建

想要制作可执行 JAR 包,在 MANIFEST.MF 中指定 Main-Class 是关键。使用 java 执行 jar 包的时候,实际上等同于使用 java 命令执行指定的 Main-Class 程序。

Spring Boot 提供了一个插件 spring-boot-maven-plugin ,用于把程序打包成一个可执行的jar包

使用 java -jar 启动 Spring Boot 的 jar 包,首先调用的入口类是 JarLauncher,内部调用 Launcher 的 launch 后构建 MainMethodRunner 对象,最终通过反射调用 HelloApplication 的 main 方法实现启动效果。

责任编辑:武晓燕 来源: Java技术指北
相关推荐

2023-09-27 08:14:56

2023-09-01 08:26:06

SpringBootjar包war包

2020-08-10 08:24:14

技术Leader代码

2023-06-02 16:24:46

SpringBootSSM

2019-05-05 09:49:17

Leader主管技术

2021-01-08 10:05:01

JavaSpring Boojar

2020-09-27 14:13:50

Spring BootJava框架

2021-10-05 22:09:31

Jar加载机制

2021-10-18 12:04:22

Spring BootJava开发

2021-10-18 10:36:31

Spring Boot插件Jar

2023-04-28 12:01:56

Spring项目编译

2022-05-09 11:01:18

配置文件数据库

2017-03-06 15:43:33

Springboot启动

2013-09-25 10:54:36

FlipboardUI设计

2022-11-18 14:15:13

2011-03-30 15:17:45

数据中心

2018-09-29 15:59:18

APPiOS优化

2021-12-31 13:40:43

Spring Boot热部署Java

2012-05-09 09:55:17

LINQ

2023-03-20 13:41:00

IoC容器Spring
点赞
收藏

51CTO技术栈公众号