为什么springboot的jar可以直接运行?


1.Spring Boot特征

在Spring Boot项目中的pom文件的常看到会说明一个插件‘spring-boot-maven-plugin’用于把程序打成一个可执行的jar包。

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

但此jar包与一般的java项目jar有所不同,此jar包可以直接通过‘java -jar xxxx.jar’命令直接运行并部署。

2.为什么?

Spring Boot应用打包之后,生成的jar可以说为Fat jar(jar包中包含jar),包含了应用依赖的jar包和Spring Boot loader相关的类。

例如 A B两个项目并且相互依赖对方。如果是平常的打包方式打成的jar包A项目jar包只会包含A项目的代码逻辑,并不会再包含B项目依赖jar包,而Spring Boot项目jar包反之。

我们可以解压并打开jar包,在BOOT-INF \ lib目录下就是该项目代码jar文件及其依赖jar包
在这里插入图片描述

3.原理

在Spring Boot项目的jar中会生成一个MANIFEST.MF文件(路径:META-INF\MANIFEST.MF),打开该文件你会看到有一个MainClass的映射,其对应的值是一个类,就是执行‘java -jar’命令后正式执行的类,mainclass类是springboot插件引入后自动添加的
在这里插入图片描述

如果想看JarLauncher类的源码需要手动引入‘spring-boot-loader’依赖,其实声明‘spring-boot-maven-plugin’插件也会自动引入其依赖,但不能查看到源码内容

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-loader</artifactId>
</dependency>

打开之后可以看到此类有一个主程序入口,main方法
在这里插入图片描述

以下就是源码解析部分

protected void launch(String[] args) throws Exception {
   JarFile.registerUrlProtocolHandler();
   //自定义类加载器加载jar文件
   ClassLoader classLoader = createClassLoader(getClassPathArchives());
   //关注getMainClass方法
   launch(args, getMainClass(), classLoader);
}

此处我们先去看下getMainClass方法,关键点在于getMainClass()方法,获取META-INF\MANIFEST.MF文件键为‘Start-Class’的值(类),并进行调用
在这里插入图片描述
ExecutableArchiveLauncher实现:

@Override
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);
   }
   return mainClass;
}

PropertiesLauncher实现:

@Override
protected String getMainClass() throws Exception {
   String mainClass = getProperty(MAIN, "Start-Class");
   if (mainClass == null) {
      throw new IllegalStateException("No '" + MAIN + "' or 'Start-Class' specified");
   }
   return mainClass;
}

再回头(码一)看launch方法

/**
 * mainClass参数就是前面getMainClass从MANIFEST.MF文件中获取到的Start-Class
 */
protected void launch(String[] args, String mainClass, ClassLoader classLoader) throws Exception {
   Thread.currentThread().setContextClassLoader(classLoader);
   createMainMethodRunner(mainClass, args, classLoader).run(); //注意此处run方法调用!!
}

MANIFEST.MF文件中的Start-Class:在这里插入图片描述

protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) {
   return new MainMethodRunner(mainClass, args);
}

最后是new了一个MainMethodRunner类,并在码四中调用run方法,而在run方法中可以看到是通过反射去执行Start-Class对于类的main方法,开始执行业务逻辑

public class MainMethodRunner {

   private final String mainClassName;

   private final String[] args;

   /**
    * Create a new {@link MainMethodRunner} instance.
    * @param mainClass the main class
    * @param args incoming arguments
    */
   public MainMethodRunner(String mainClass, String[] args) {
      this.mainClassName = mainClass;
      this.args = (args != null) ? args.clone() : null;
   }

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

}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值