springboot的类加载器(org.springframework.boot.loader)过程详解
作者:EasyChill 发布时间:2021-05-23 21:01:44
类加载器的分类。
试验:使用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文档
来源:https://blog.csdn.net/Song_JiangTao/article/details/109510484


猜你喜欢
- 在 C# 中,程序中在运行时出现的错误,会不断在程序中进行传播,这种机制称为“异常”。异常通常由错误的代码引发,并由能够更正错误的代码进行
- 本文实例讲述了C#利用System.Uri转URL为绝对地址的方法。分享给大家供大家参考。具体分析如下:在使用ASPOSE.Word生成Wo
- 前言本篇开始讲解音频编辑的具体操作,从相对简单的音频裁剪开始。要进行音频裁剪,我的方案是开启一个Service服务用于音频裁剪的耗时操作,主
- Java 反射机制介绍Java 反射机制。通俗来讲呢,就是在运行状态中,我们可以根据“类的部分已经的信息”来还原“类的全部的信息”。这里“类
- 定义队列是 Apache RocketMQ 中消息存储和传输的实际容器,也是 Apache RocketMQ 消息的最小存储单元。 Apac
- 现象: 1. 表面现象: 方法中输出的日志, 日志文件中找不到, 也没有任何报错(即@Async标注的方法没有执行, 也没有报错)2. 分析
- 本文实例为大家分享了C#实现围棋游戏的具体代码,供大家参考,具体内容如下之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从6岁学围
- 本文以Android签名JKS格式的证书为例:package com.test;import java.io.FileInputStream
- 1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool
- 1、Dom4j概述dom4j is an easy to use, open source library for working with
- MyBatis中PageHelper不生效今天使用pageHelper,发现设置了PageHelper.startPage(page, pa
- 前言日志,在我们开发中是一个非常重要的话题,良好的日志打印可以帮助我们快速的定位问题,可能现在我们开发用到最多的日志框架就是slf4j了,但
- 创建一个列表消息卡片到目前为止,我们只有一个消息的卡片,看上去有点单调,所以让我们来改善它,让它拥有多条信息。我们需要创建一个能够显示多条消
- 目前在公司做一个小东西,里面用到了 FFmpeg 简单处理音视频,感觉功能特别强大,在做之前我写了一个小例子,现在记录一下分享给大家,希望大
- 一、问题描述有时候,我们会遇到在遍历List集合的过程中删除数据的情况。看着自己写的代码,感觉完全没有问题,但就是达不到预期的效果,这是为什
- 关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较
- 本文实例为大家分享了C#简单聊天室雏形的具体代码,供大家参考,具体内容如下程序使用的控制台的黑窗口模拟程序,第一次涉及网络编程,写出来方便以
- Map集合Map集合存储的是键值对Map集合的实现类:HashTable、LinkedHashMap、HashMap、TreeMapHash
- 本文实例为大家分享了C#实现钟表程序设计的具体代码,供大家参考,具体内容如下工作空间:代码如下:using System;using Sys
- 现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用USB蓝牙方便的连接手机等蓝牙设备进行通信。操作蓝牙要使用类库InTh