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

 更新时间:2020年11月04日 13:56:12   作者:EasyChill  
这篇文章主要介绍了springboot的类加载器(org.springframework.boot.loader),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

类加载器的分类。

在这里插入图片描述

试验:使用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文档

到此这篇关于springboot的类加载器(org.springframework.boot.loader)的文章就介绍到这了,更多相关springboot类加载器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • IntelliJ IDEA 好用插件之analyze inspect code详解

    IntelliJ IDEA 好用插件之analyze inspect code详解

    这篇文章主要介绍了IntelliJ IDEA 好用插件之analyze inspect code的相关知识,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-12-12
  • Java类的加载连接和初始化实例分析

    Java类的加载连接和初始化实例分析

    这篇文章主要介绍了Java类的加载连接和初始化,结合具体实例形式分析了java类的加载、连接、初始化相关原理与实现技巧,需要的朋友可以参考下
    2019-07-07
  • Java检查字符串是否一致的四种方法

    Java检查字符串是否一致的四种方法

    字符串比较是常见的操作,包括比较相等、比较大小、比较前缀和后缀串等,在 Java 中,比较字符串的常用方法有四个:equals(),equalsIgnoreCase(),compareTo()和compareToIgnoreCase(),下面详细介绍这四个方法的使用
    2024-04-04
  • spring data jpa查询一个实体类的部分属性方式

    spring data jpa查询一个实体类的部分属性方式

    这篇文章主要介绍了spring data jpa查询一个实体类的部分属性方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • SpringSecurity请求授权规则配置与注解详解

    SpringSecurity请求授权规则配置与注解详解

    这篇文章主要介绍了SpringSecurity请求授权规则配置与注解详解,我们常使用@Secured与@PreAuthorize两个注解在进入方法前进行角色、权限的控制,进入方法前数据的过滤@PreFilter注解偶尔会看到,需要的朋友可以参考下
    2023-12-12
  • Spring Security过滤器链体系的实例详解

    Spring Security过滤器链体系的实例详解

    这篇文章主要介绍了Spring Security过滤器链体系,通过思维导图可以很好的帮助大家理解配置类的相关知识,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-02-02
  • Spring AOP 创建代理对象详情

    Spring AOP 创建代理对象详情

    这篇文章介绍了Spring AOP 创建代理对象详情,主要介绍AOP 创建代理对象和上下文相关的内容,下文分享具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05
  • 整理Java编程中常用的基本描述符与运算符

    整理Java编程中常用的基本描述符与运算符

    这篇文章主要介绍了Java编程中常用的基本描述符与运算符整理,包括运算符优先级等平时经常查看反复记忆的知识点,需要的朋友可以参考下
    2016-02-02
  • 简单实现Java web服务器

    简单实现Java web服务器

    这篇文章主要为大家详细介绍了简单实现Java web服务器的详细步骤,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Java使用Socket通信传输文件的方法示例

    Java使用Socket通信传输文件的方法示例

    这篇文章主要介绍了Java使用Socket通信传输文件的方法,结合实例形式分析了java socket编程实现文件传输操作的相关技巧,需要的朋友可以参考下
    2017-06-06

最新评论