springboot的類加載器(org.springframework.boot.loader)過程詳解
類加載器的分類。
試驗(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)程序,用于通過屬性文件使用用戶配置的類路徑和主類進(jìn)行歸檔。與基于可執(zhí)行jar的模型相比,這種模型通常更靈活,更易于創(chuàng)建性能良好的OS級(jí)服務(wù)。
在不同位置查找屬性文件以提取加載程序設(shè)置,默認(rèn)為應(yīng)用程序.屬性在當(dāng)前類路徑或當(dāng)前工作目錄中。屬性文件的名稱可以通過設(shè)置系統(tǒng)屬性來更改加載程序.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í)行委托給的主方法。沒有默認(rèn)值,但將返回到在清單.MF,如果有${裝載機(jī).home}/中導(dǎo)。
這種也是配置:使用jar包外部的配置文件的啟動(dòng)方式的方法。在linux我們的啟動(dòng)腳本要使用對(duì)應(yīng)的類加載器來啟動(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打包出來的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)文章
Java中OAuth2.0第三方授權(quán)原理與實(shí)戰(zhàn)
本文主要介紹了Java中OAuth2.0第三方授權(quán)原理與實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05java IO流將一個(gè)文件拆分為多個(gè)子文件代碼示例
這篇文章主要介紹了java IO流將一個(gè)文件拆分為多個(gè)子文件代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12java調(diào)用shell腳本及注意事項(xiàng)說明
這篇文章主要介紹了java調(diào)用shell腳本及注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06自己動(dòng)手寫的mybatis分頁(yè)插件(極其簡(jiǎn)單好用)
最近做了個(gè)項(xiàng)目,需要用到mybatis分頁(yè)功能,網(wǎng)上找了很多插件,都不太合適,于是就自己動(dòng)手寫了個(gè)mybatis分頁(yè)插件功能,非常不錯(cuò),代碼簡(jiǎn)單易懂,需要的朋友參考下吧2016-11-11Maven項(xiàng)目部署到Jboss出現(xiàn)Failed to create a new SAX parser
這篇文章主要為大家詳細(xì)介紹了Maven項(xiàng)目部署到Jboss出現(xiàn)Failed to create a new SAX parser的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11