java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot Jar包

Spring Boot 打包成Jar包运行原理分析

作者:Mr_姚

这篇文章主要为大家介绍了Spring Boot 打包成Jar包运行的原理分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

SpringBoot打包

相比与传统的Java打包方式,使用SpringBoot打包插件打包成jar包后,可以直接使用java -jar 运行SpringBoot项目,本篇就来分析一下运行的原理。

SpringBoot打包插件

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

打包完后会生成两个文件,一个***.jar***.jar.original

.jar文件是SpringBoot打包后生成的文件,.jar.original是用原生方式打包生成的文件,对比一下两个的区别

.jar.original文件

.jar文件

.jar.original就是普通的jar打包的结构,这里主要看.jar文件的结构:

重点 META-INFO/MANIFEST.MF

Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Start-Class: com.example.demo.DemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.4.5
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher

这里有几个重点的字段

有了Main-Class启动类,那就直接进入到JarLauncher里查看运行的过程

    public static void main(String[] args) throws Exception {
        (new JarLauncher()).launch(args);
    }

JarLaunchermain方法里调用了launch方法,launch方法的具体实现在JarLauncher的抽象父类Launcher中实现

    protected void launch(String[] args) throws Exception {
        if (!this.isExploded()) {
            JarFile.registerUrlProtocolHandler();
        }
        ClassLoader classLoader = this.createClassLoader(this.getClassPathArchivesIterator());
        String jarMode = System.getProperty("jarmode");
        String launchClass = jarMode != null && !jarMode.isEmpty() ? "org.springframework.boot.loader.jarmode.JarModeLauncher" : this.getMainClass();
        this.launch(args, launchClass, classLoader);
    }

首先获取了类加载器。

然后获取jarMode,再根据jarMode获取launchClass,如果没有设置jarMode,则根据getMainClass方法获取,getMainClass的具体实现在ExecutableArchiveLauncher中实现

 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);
        } else {
            return mainClass;
        }
    }

getMainClass里获取了MANIFEST.MF文件里Start-Class字段的值,也就是本地项目的启动类。

最后调用this.launch(args, launchClass, classLoader);

    protected void launch(String[] args, String launchClass, ClassLoader classLoader) throws Exception {
        Thread.currentThread().setContextClassLoader(classLoader);
        this.createMainMethodRunner(launchClass, args, classLoader).run();
    }

调用MainMethodRunnerrun()方法

    public void run() throws Exception {
        Class<?> mainClass = Class.forName(this.mainClassName, false, Thread.currentThread().getContextClassLoader());
        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
        mainMethod.setAccessible(true);
        mainMethod.invoke((Object)null, this.args);
    }

run()方法里通过反射拿到了项目的启动类的main方法,从而启动本地项目。

以上就是Spring Boot 打包成Jar包运行的原理的详细内容,更多关于Spring Boot Jar包的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文