您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)springboot中的(org.springframework.boot.loader)類加載器實(shí)現(xiàn)過程,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
類加載器的分類。
試驗(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>啟動類的完整路徑</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>
各個配置項(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.
啟動程序,用于通過屬性文件使用用戶配置的類路徑和主類進(jìn)行歸檔。與基于可執(zhí)行jar的模型相比,這種模型通常更靈活,更易于創(chuàng)建性能良好的OS級服務(wù)。
在不同位置查找屬性文件以提取加載程序設(shè)置,默認(rèn)為應(yīng)用程序.屬性在當(dāng)前類路徑或當(dāng)前工作目錄中。屬性文件的名稱可以通過設(shè)置系統(tǒng)屬性來更改加載程序.config.name(例如-Dloader.config.name=foo會尋找食品屬性. 如果該文件不存在,則嘗試loader.config.location(使用允許的前綴classpath:和file:或任何有效的URL)。找到該文件后,將其轉(zhuǎn)換為屬性并提取可選值(如果該文件不存在,也可以將其作為系統(tǒng)屬性進(jìn)行覆蓋):
加載程序.path:以逗號分隔的目錄列表(包含文件資源和/或.jar或.zip中的嵌套存檔文件)或要附加到類路徑的存檔文件??偸鞘褂脩?yīng)用程序檔案中的BOOT-INF/classes、BOOT-INF/lib
裝載機(jī).main:設(shè)置類裝入器后將執(zhí)行委托給的主方法。沒有默認(rèn)值,但將返回到在清單.MF,如果有${裝載機(jī).home}/中導(dǎo)。
這種也是配置:使用jar包外部的配置文件的啟動方式的方法。在linux我們的啟動腳本要使用對應(yīng)的類加載器來啟動。
如果你配置成其他的( <layout>ZIP</layout>
此配置項(xiàng)去掉,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è)依賴項(xiàng)jar包含在/BOOT-INF/lib目錄中,應(yīng)用程序類包含在/BOOT-INF/classes目錄中。
就會有如下的錯誤:項(xiàng)目啟動失敗。
springboot項(xiàng)目啟動,調(diào)用的是相應(yīng)的類加載器的main方法,而不是我們自己編寫的SpringApplication
Spring Boot Loader提供了一套標(biāo)準(zhǔn)用于執(zhí)行SpringBoot打包出來的jar,這套標(biāo)準(zhǔn)就是我們的類加載器
下面是一個例子,圖中有構(gòu)造方法,和類方法。
看完上述內(nèi)容,你們對springboot中的(org.springframework.boot.loader)類加載器實(shí)現(xiàn)過程有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。