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.
啟動程序,用于通過屬性文件使用用戶配置的類路徑和主類進行歸檔。與基于可執(zhí)行jar的模型相比,這種模型通常更靈活,更易于創(chuàng)建性能良好的OS級服務(wù)。
在不同位置查找屬性文件以提取加載程序設(shè)置,默認為應(yīng)用程序.屬性在當前類路徑或當前工作目錄中。屬性文件的名稱可以通過設(shè)置系統(tǒng)屬性來更改加載程序.config.name(例如-Dloader.config.name=foo會尋找食品屬性. 如果該文件不存在,則嘗試loader.config.location(使用允許的前綴classpath:和file:或任何有效的URL)。找到該文件后,將其轉(zhuǎn)換為屬性并提取可選值(如果該文件不存在,也可以將其作為系統(tǒng)屬性進行覆蓋):
加載程序.path:以逗號分隔的目錄列表(包含文件資源和/或.jar或.zip中的嵌套存檔文件)或要附加到類路徑的存檔文件??偸鞘褂脩?yīng)用程序檔案中的BOOT-INF/classes、BOOT-INF/lib
裝載機.main:設(shè)置類裝入器后將執(zhí)行委托給的主方法。沒有默認值,但將返回到在清單.MF,如果有${裝載機.home}/中導。
這種也是配置:使用jar包外部的配置文件的啟動方式的方法。在linux我們的啟動腳本要使用對應(yīng)的類加載器來啟動。

如果你配置成其他的( <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的檔案的啟動程序。這個啟動程序假設(shè)依賴項jar包含在/BOOT-INF/lib目錄中,應(yīng)用程序類包含在/BOOT-INF/classes目錄中。
就會有如下的錯誤:項目啟動失敗。

springboot項目啟動,調(diào)用的是相應(yīng)的類加載器的main方法,而不是我們自己編寫的SpringApplication
Spring Boot Loader提供了一套標準用于執(zhí)行SpringBoot打包出來的jar,這套標準就是我們的類加載器
下面是一個例子,圖中有構(gòu)造方法,和類方法。

友情鏈接
到此這篇關(guān)于springboot的類加載器(org.springframework.boot.loader)的文章就介紹到這了,更多相關(guān)springboot類加載器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解
這篇文章主要介紹了IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12
SpringSecurity請求授權(quán)規(guī)則配置與注解詳解
這篇文章主要介紹了SpringSecurity請求授權(quán)規(guī)則配置與注解詳解,我們常使用@Secured與@PreAuthorize兩個注解在進入方法前進行角色、權(quán)限的控制,進入方法前數(shù)據(jù)的過濾@PreFilter注解偶爾會看到,需要的朋友可以參考下2023-12-12

