自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我說 Spring Boot 不能用 Jar 包啟動?leader 過來就是一 jo。。。

開發(fā) 架構(gòu)
Spring Boot 提供了一個名叫 spring-boot-maven-plugin 的 maven 項目打包插件,可以方便的將 Spring Boot 項目打成 jar 包。這樣我們就不再需要部署 Tomcat 、Jetty等之類的 Web 服務(wù)器容器啦。

[[407460]]

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

可能很多初學(xué)者會比較困惑,Spring Boot 是如何做到將應(yīng)用代碼和所有的依賴打包成一個獨立的 Jar 包,因為傳統(tǒng)的 Java 項目打包成 Jar 包之后,需要通過 -classpath 屬性來指定依賴,才能夠運行。我們今天就來分析講解一下 Spring Boot 的啟動原理。

1. Spring Boot 打包插件

Spring Boot 提供了一個名叫 spring-boot-maven-plugin 的 maven 項目打包插件,可以方便的將 Spring Boot 項目打成 jar 包。這樣我們就不再需要部署 Tomcat 、Jetty等之類的 Web 服務(wù)器容器啦。

我們先看一下 Spring Boot 打包后的結(jié)構(gòu)是什么樣的,打開 target 目錄我們發(fā)現(xiàn)有兩個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原生的打包方式生成的,僅僅只包含了項目本身的內(nèi)容。

2. SpringBoot FatJar 的組織結(jié)構(gòu)

我們將 Spring Boot 打的可執(zhí)行 Jar 展開后的結(jié)構(gòu)如下所示:

  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包的元數(shù)據(jù),聲明了 jar 的啟動類
  • org.springframework.boot.loader :Spring Boot 的加載器代碼,實現(xiàn)的 Jar in Jar 加載的魔法源

我們看到,如果去掉BOOT-INF目錄,這將是一個非常普通且標(biāo)準(zhǔn)的Jar包,包括元信息以及可執(zhí)行的代碼部分,其/META-INF/MAINFEST.MF指定了Jar包的啟動元信息,org.springframework.boot.loader 執(zhí)行對應(yīng)的邏輯操作。

3. MAINFEST.MF 元信息分析

元信息內(nèi)容如下所示:

  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 

它相當(dāng)于一個 Properties 配置文件,每一行都是一個配置項目。重點來看看兩個配置項:

  • Main-Class 配置項:Java 規(guī)定的 jar 包的啟動類,這里設(shè)置為 spring-boot-loader 項目的 JarLauncher 類,進行 Spring Boot 應(yīng)用的啟動。
  • Start-Class 配置項:Spring Boot 規(guī)定的主啟動類,這里設(shè)置為我們定義的 Application 類。
  • Spring-Boot-Classes 配置項:指定加載應(yīng)用類的入口
  • Spring-Boot-Lib 配置項: 指定加載應(yīng)用依賴的庫

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.     } 

當(dāng)執(zhí)行 java -jar 命令或執(zhí)行解壓后的 org.springframework.boot.loader.JarLauncher 類時,JarLauncher 會將 BOOT-INF/classes 下的類文件和 BOOT-INF/lib 下依賴的jar加入到classpath下,后調(diào)用 META-INF/MANIFEST.MF 文件 Start-Class 屬性 [指向項目中的 com.javanorth.hello.HelloApplicatioin 啟動類] 完成應(yīng)用程序的啟動。

JarLauncher 假定依賴項jar包含在 /BOOT-INF/lib 目錄中,并且應(yīng)用程序類包含在 /BOOT-INF/classes 目錄中。它的 main 方法調(diào)用的則是基類 Launcher 定義的 launch 方法,而 Launcher 是ExecutableArchiveLauncher 的父類。

5.2 org.springframework.boot.loader.ExecutableArchiveLauncher

ExecutableArchiveLauncher 是 JarLauncher 的直接父類,繼承了 Launcher 基類,并實現(xiàn)部分抽象方法

  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 方法會首先創(chuàng)建類加載器,而后判斷 jar 是否在 MANIFEST.MF 文件中設(shè)置了 jarmode 屬性。
  2. 如果沒有設(shè)置,launchClass 的值就來自 getMainClass() 返回,該方法由子類實現(xiàn),返回 MANIFEST.MF 中配置的 START_CLASS_ATTRIBUTE 屬性值
  3. 調(diào)用 createMainMethodRunner 方法,構(gòu)建一個 MainMethodRunner 對象并調(diào)用其 run 方法

jarmode 是創(chuàng)建 docker 鏡像時用到的參數(shù),使用該參數(shù)是為了生成帶有多個 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

從名字可以判斷這是一個目標(biāo)類main方法的執(zhí)行器,此時的 mainClassName 被賦值為 MANIFEST.MF 中配置的 START_CLASS_ATTRIBUTE 屬性值,也就是 com.javanorth.hello.HelloApplication,之后便是通過反射執(zhí)行 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.     } 

總結(jié)

jar 包類似于 zip 壓縮文件,只不過相比 zip 文件多了一個 META-INF/MANIFEST.MF 文件,該文件在構(gòu)建 jar 包時自動創(chuàng)建

想要制作可執(zhí)行 JAR 包,在 MANIFEST.MF 中指定 Main-Class 是關(guān)鍵。使用 java 執(zhí)行 jar 包的時候,實際上等同于使用 java 命令執(zhí)行指定的 Main-Class 程序。

Spring Boot 提供了一個插件 spring-boot-maven-plugin ,用于把程序打包成一個可執(zhí)行的jar包

使用 java -jar 啟動 Spring Boot 的 jar 包,首先調(diào)用的入口類是 JarLauncher,內(nèi)部調(diào)用 Launcher 的 launch 后構(gòu)建 MainMethodRunner 對象,最終通過反射調(diào)用 HelloApplication 的 main 方法實現(xiàn)啟動效果。

責(zé)任編輯:武曉燕 來源: Java技術(shù)指北
相關(guān)推薦

2023-09-27 08:14:56

2020-08-10 08:24:14

技術(shù)Leader代碼

2019-05-05 09:49:17

Leader主管技術(shù)

2023-06-02 16:24:46

SpringBootSSM

2024-12-16 08:10:00

Spring開發(fā)

2023-09-01 08:26:06

SpringBootjar包war包

2021-01-08 10:05:01

JavaSpring Boojar

2024-08-09 08:46:00

Springjar 包YAML

2020-09-27 14:13:50

Spring BootJava框架

2021-10-05 22:09:31

Jar加載機制

2023-04-28 12:01:56

Spring項目編譯

2022-05-09 11:01:18

配置文件數(shù)據(jù)庫

2021-10-18 12:04:22

Spring BootJava開發(fā)

2021-10-18 10:36:31

Spring Boot插件Jar

2025-02-06 16:58:30

2017-03-06 15:43:33

Springboot啟動

2013-09-25 10:54:36

FlipboardUI設(shè)計

2022-11-18 14:15:13

2024-11-26 08:36:56

SpringJar機制

2024-07-09 00:00:02

監(jiān)聽類Spring事件
點贊
收藏

51CTO技術(shù)棧公眾號