springboot的類加載器(org.springframework.boot.loader)過(guò)程詳解
類加載器的分類。

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

如果你配置成其他的( <layout>ZIP</layout>此配置項(xiàng)去掉,main-class就會(huì)變成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的檔案的啟動(dòng)程序。這個(gè)啟動(dòng)程序假設(shè)依賴項(xiàng)jar包含在/BOOT-INF/lib目錄中,應(yīng)用程序類包含在/BOOT-INF/classes目錄中。
就會(huì)有如下的錯(cuò)誤:項(xiàng)目啟動(dòng)失敗。

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

友情鏈接
到此這篇關(guān)于springboot的類加載器(org.springframework.boot.loader)的文章就介紹到這了,更多相關(guān)springboot類加載器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解
這篇文章主要介紹了IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code的相關(guān)知識(shí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12
spring?data?jpa查詢一個(gè)實(shí)體類的部分屬性方式
這篇文章主要介紹了spring?data?jpa查詢一個(gè)實(shí)體類的部分屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解
這篇文章主要介紹了SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解,我們常使用@Secured與@PreAuthorize兩個(gè)注解在進(jìn)入方法前進(jìn)行角色、權(quán)限的控制,進(jìn)入方法前數(shù)據(jù)的過(guò)濾@PreFilter注解偶爾會(huì)看到,需要的朋友可以參考下2023-12-12
Spring?Security過(guò)濾器鏈體系的實(shí)例詳解
這篇文章主要介紹了Spring?Security過(guò)濾器鏈體系,通過(guò)思維導(dǎo)圖可以很好的幫助大家理解配置類的相關(guān)知識(shí),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Spring?AOP?創(chuàng)建代理對(duì)象詳情
這篇文章介紹了Spring?AOP?創(chuàng)建代理對(duì)象詳情,主要介紹AOP?創(chuàng)建代理對(duì)象和上下文相關(guān)的內(nèi)容,下文分享具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
簡(jiǎn)單實(shí)現(xiàn)Java web服務(wù)器
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)Java web服務(wù)器的詳細(xì)步驟,感興趣的小伙伴們可以參考一下2016-06-06

