日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
我說(shuō)SpringBoot不能用Jar包啟動(dòng)?leader過(guò)來(lái)就是一jo。。。

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

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)武邑,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792

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

1. Spring Boot 打包插件

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

我們先看一下 Spring Boot 打包后的結(jié)構(gòu)是什么樣的,打開(kāi) target 目錄我們發(fā)現(xiàn)有兩個(gè)jar包:

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

其中,hello-0.0.1-SNAPSHOT.jar 是通過(guò) Spring Boot 提供的打包插件采用新的格式打成 Fat Jar,包含了所有的依賴;而 hello-0.0.1-SNAPSHOT.jar.original 則是Java原生的打包方式生成的,僅僅只包含了項(xiàng)目本身的內(nèi)容。

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

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

 
 
 
 
  1. .
  2. ├── BOOT-INF
  3. │   ├── classes
  4. │   │   ├── application.properties
  5. │   │   └── com
  6. │   │       └── javanorth
  7. │   │           └── hello
  8. │   │               └── HelloApplication.class
  9. │   └── lib
  10. │       ├── spring-boot-2.5.0.RELEASE.jar
  11. │       ├── spring-boot-autoconfigure-2.5.0.RELEASE.jar
  12. │       ├── spring-boot-configuration-processor-2.5.0.RELEASE.jar
  13. │       ├── spring-boot-starter-2.5.0.RELEASE.jar
  14. │       ├── ...
  15. ├── META-INF
  16. │   ├── MANIFEST.MF
  17. │   └── maven
  18. │       └── com.javanorth
  19. │           └── hello
  20. │               ├── pom.properties
  21. │               └── pom.xml
  22. │   
  23. ├── org
  24. │   └── springframework
  25. │       └── boot
  26. │           └── loader
  27. │               ├── ExecutableArchiveLauncher.class
  28. │               ├── JarLauncher.class
  29. │               ├── Launcher.class
  30. │               ├── MainMethodRunner.class
  31. │               ├── ...
  • BOOT-INF目錄:包含了我們的項(xiàng)目代碼(classes目錄),以及所需要的依賴(lib 目錄)
  • META-INF目錄:通過(guò) MANIFEST.MF 文件提供 Jar包的元數(shù)據(jù),聲明了 jar 的啟動(dòng)類
  • org.springframework.boot.loader :Spring Boot 的加載器代碼,實(shí)現(xiàn)的 Jar in Jar 加載的魔法源

我們看到,如果去掉BOOT-INF目錄,這將是一個(gè)非常普通且標(biāo)準(zhǔn)的Jar包,包括元信息以及可執(zhí)行的代碼部分,其/META-INF/MAINFEST.MF指定了Jar包的啟動(dòng)元信息,org.springframework.boot.loader 執(zhí)行對(duì)應(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)于一個(gè) Properties 配置文件,每一行都是一個(gè)配置項(xiàng)目。重點(diǎn)來(lái)看看兩個(gè)配置項(xiàng):

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

4. 啟動(dòng)原理

Spring Boot 的啟動(dòng)原理如下圖所示:

5. 源碼分析

5.1 org.springframework.boot.loader.JarLauncher

JarLauncher 類是針對(duì) Spring Boot jar 包的啟動(dòng)類, 完整的類圖如下所示:

Spring Boot Start jar 2

其中的 WarLauncher 類,是針對(duì) Spring Boot war 包的啟動(dòng)類。啟動(dòng)類 org.springframework.boot.loader.JarLauncher 并非為項(xiàng)目中引入類,而是 spring-boot-maven-plugin 插件 repackage 追加進(jìn)去的。接下來(lái)我們先來(lái)看一下 JarLauncher 的源碼,比較簡(jiǎn)單,如下圖所示:

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

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

JarLauncher 假定依賴項(xiàng)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 基類,并實(shí)現(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 archives) throws Exception {
  41.         List 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 getClassPathArchivesIterator() throws Exception {
  58.         Archive.EntryFilter searchFilter = this::isSearchCandidate;
  59.         Iterator 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 applyClassPathArchivePostProcessing(Iterator archives) throws Exception {
  73.         List 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 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.     }
  97. }

5.3 org.springframework.boot.loader.Launcher

如下則是 Launcher 的源碼

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

jarmode 是創(chuàng)建 docker 鏡像時(shí)用到的參數(shù),使用該參數(shù)是為了生成帶有多個(gè) 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 archives) throws Exception {
  14.         return createClassLoader(archives.iterator());
  15.     }
  16.     protected ClassLoader createClassLoader(Iterator archives) throws Exception {
  17.         List 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 getClassPathArchivesIterator() throws Exception {
  35.         return getClassPathArchives().iterator();
  36.     }
  37.     @Deprecated
  38.     protected List 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.     }
  61. }

5.4 org.springframework.boot.loader.MainMethodRunner

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

總結(jié)

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

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

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

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


網(wǎng)站欄目:我說(shuō)SpringBoot不能用Jar包啟動(dòng)?leader過(guò)來(lái)就是一jo。。。
文章來(lái)源:http://m.5511xx.com/article/cojdees.html