springboot的类加载器(org.springframework.boot.loader)过程详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot的类加载器(org.springframework.boot.loader)过程详解

EasyChill   2020-11-05 我要评论

类加载器的分类。

在这里插入图片描述

试验:使用maven打包

<build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
     <execution>
      <id>copy-dependencies</id>
      <phase>prepare-package</phase>
      <goals>
       <goal>copy-dependencies</goal>
      </goals>
      <configuration>
       <outputDirectory>${project.build.directory}/lib</outputDirectory>
       <overWriteReleases>false</overWriteReleases>
       <overWriteSnapshots>false</overWriteSnapshots>
       <overWriteIfNewer>true</overWriteIfNewer>
      </configuration>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <fork>true</fork>
     <mainClass>启动类的完整路径</mainClass>
     <excludes>
      <exclude>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
      </exclude>
     </excludes>
     <layout>ZIP</layout>
     <includes>
      <include>
       <groupId>nothing</groupId>
       <artifactId>nothing</artifactId>
      </include>
     </includes>
    </configuration>
    <executions>
     <execution>
      <goals>
       <goal>repackage</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

各个配置项的作用自行百度~

这样生成的jar包,用压缩工具打开以后是这样

在这里插入图片描述

在META-INF下打开MANIFEST.MF文件看看(记事本就可以打开)

在这里插入图片描述

可以看到这里的类加载器是:PropertiesLauncher,文档百度翻译如下:

Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.
Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn't exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn't exist):
loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used
loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.

启动程序,用于通过属性文件使用用户配置的类路径和主类进行归档。与基于可执行jar的模型相比,这种模型通常更灵活,更易于创建性能良好的OS级服务。

在不同位置查找属性文件以提取加载程序设置,默认为应用程序.属性在当前类路径或当前工作目录中。属性文件的名称可以通过设置系统属性来更改加载程序.config.name(例如-Dloader.config.name=foo会寻找食品属性. 如果该文件不存在,则尝试loader.config.location(使用允许的前缀classpath:和file:或任何有效的URL)。找到该文件后,将其转换为属性并提取可选值(如果该文件不存在,也可以将其作为系统属性进行覆盖):

加载程序.path:以逗号分隔的目录列表(包含文件资源和/或.jar或.zip中的嵌套存档文件)或要附加到类路径的存档文件。总是使用应用程序档案中的BOOT-INF/classes、BOOT-INF/lib

装载机.main:设置类装入器后将执行委托给的主方法。没有默认值,但将返回到在清单.MF,如果有${装载机.home}/中导。

这种也是配置:使用jar包外部的配置文件的启动方式的方法。在linux我们的启动脚本要使用对应的类加载器来启动。

在这里插入图片描述

如果你配置成其他的( <layout>ZIP</layout>此配置项去掉,main-class就会变成JarLauncher)比如:JarLauncher

Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.

基于JAR的档案的启动程序。这个启动程序假设依赖项jar包含在/BOOT-INF/lib目录中,应用程序类包含在/BOOT-INF/classes目录中。

就会有如下的错误:项目启动失败。

在这里插入图片描述

springboot项目启动,调用的是相应的类加载器的main方法,而不是我们自己编写的SpringApplication

Spring Boot Loader提供了一套标准用于执行SpringBoot打包出来的jar,这套标准就是我们的类加载器

下面是一个例子,图中有构造方法,和类方法。

在这里插入图片描述

友情链接

springboot整个api

PropertiesLauncher文档

JarLauncher文档

WarLauncher文档

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们