Springboot源碼分析之jar探秘
- 2019 年 10 月 3 日
- 筆記
摘要:
- 利用IDEA等工具打包會出現springboot-0.0.1-SNAPSHOT.jar,springboot-0.0.1-SNAPSHOT.jar.original,前面說過它們之間的關係了,接下來我們就一探究竟,它們之間到底有什麼聯繫。
文件對比:
-
進入target目錄,unzip springboot-0.0.1-SNAPSHOT.jar -d jar命令將springboot-0.0.1-SNAPSHOT.jar解壓到jar目錄
-
進入target目錄,unzip springboot-0.0.1-SNAPSHOT.jar.original -d original命令將springboot-0.0.1-SNAPSHOT.jar.original解壓到original目錄
前面文章分析過springboot-0.0.1-SNAPSHOT.jar.original不能執行,將它進行repackage後生成springboot-0.0.1-SNAPSHOT.jar就成了我們的可執行fat jar,對比上面文件會發現可執行 fat jar和original jar目錄不一樣,最關鍵的地方是多了org.springframework.boot.loader這個包,這個就是我們平時java -jar springboot-0.0.1-SNAPSHOT.jar命令啟動的奧妙所在。MANIFEST.MF文件裡面的內容包含了很多關鍵的資訊
Manifest-Version: 1.0 Start-Class: com.github.dqqzj.springboot.SpringbootApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Build-Jdk-Spec: 1.8 Spring-Boot-Version: 2.1.6.RELEASE Created-By: Maven Archiver 3.4.0 Main-Class: org.springframework.boot.loader.JarLauncher
相信不用多說大家都能明白Main-Class: org.springframework.boot.loader.JarLauncher是我們 java -jar命令啟動的入口,後續會進行分析,Start-Class: com.github.dqqzj.springboot.SpringbootApplication才是我們程式的入口主函數。
Springboot jar啟動源碼分析
public class JarLauncher extends ExecutableArchiveLauncher { static final String BOOT_INF_CLASSES = "BOOT-INF/classes/"; static final String BOOT_INF_LIB = "BOOT-INF/lib/"; public JarLauncher() { } protected JarLauncher(Archive archive) { super(archive); } /** * 判斷是否歸檔文件還是文件系統的目錄 可以猜想基於文件系統一樣是可以啟動的 */ protected boolean isNestedArchive(Entry entry) { return entry.isDirectory() ? entry.getName().equals("BOOT-INF/classes/") : entry.getName().startsWith("BOOT-INF/lib/"); } public static void main(String[] args) throws Exception { /** * 進入父類初始化構造器ExecutableArchiveLauncher * launch方法交給Launcher執行 */ (new JarLauncher()).launch(args); } } public abstract class ExecutableArchiveLauncher extends Launcher { private final Archive archive; public ExecutableArchiveLauncher() { try { /** * 使用父類Launcher載入資源,包括BOOT-INF的classes和lib下面的所有歸檔文件 */ this.archive = this.createArchive(); } catch (Exception var2) { throw new IllegalStateException(var2); } } protected ExecutableArchiveLauncher(Archive archive) { this.archive = archive; } protected final Archive getArchive() { return this.archive; } /** * 從歸檔文件中獲取我們的應用程式主函數 */ protected String getMainClass() throws Exception { Manifest manifest = this.archive.getManifest(); String mainClass = null; if (manifest != null) { mainClass = manifest.getMainAttributes().getValue("Start-Class"); } if (mainClass == null) { throw new IllegalStateException("No 'Start-Class' manifest entry specified in " + this); } else { return mainClass; } } protected List<Archive> getClassPathArchives() throws Exception { List<Archive> archives = new ArrayList(this.archive.getNestedArchives(this::isNestedArchive)); this.postProcessClassPathArchives(archives); return archives; } protected abstract boolean isNestedArchive(Entry entry); protected void postProcessClassPathArchives(List<Archive> archives) throws Exception { } } public abstract class Launcher { public Launcher() { } protected void launch(String[] args) throws Exception { /** *註冊協議處理器,由於Springboot是 jar in jar 所以要重寫jar協議才能讀取歸檔文件 */ JarFile.registerUrlProtocolHandler(); ClassLoader classLoader = this.createClassLoader(this.getClassPathArchives()); /** * this.getMainClass()交給子類ExecutableArchiveLauncher實現 */ this.launch(args, this.getMainClass(), classLoader); } protected ClassLoader createClassLoader(List<Archive> archives) throws Exception { List<URL> urls = new ArrayList(archives.size()); Iterator var3 = archives.iterator(); while(var3.hasNext()) { Archive archive = (Archive)var3.next(); urls.add(archive.getUrl()); } return this.createClassLoader((URL[])urls.toArray(new URL[0])); } /** * 該類載入器是fat jar的關鍵的一處,因為傳統的類載入器無法讀取jar in jar模型,所以springboot進行了自己實現 */ protected ClassLoader createClassLoader(URL[] urls) throws Exception { return new LaunchedURLClassLoader(urls, this.getClass().getClassLoader()); } protected void launch(String[] args, String mainClass, ClassLoader classLoader) throws Exception { Thread.currentThread().setContextClassLoader(classLoader); this.createMainMethodRunner(mainClass, args, classLoader).run(); } /** * 創建應用程式主函數運行器 */ protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) { return new MainMethodRunner(mainClass, args); } protected abstract String getMainClass() throws Exception; protected abstract List<Archive> getClassPathArchives() throws Exception; /** * 得到我們的啟動jar的歸檔文件 */ protected final Archive createArchive() throws Exception { ProtectionDomain protectionDomain = this.getClass().getProtectionDomain(); CodeSource codeSource = protectionDomain.getCodeSource(); URI location = codeSource != null ? codeSource.getLocation().toURI() : null; String path = location != null ? location.getSchemeSpecificPart() : null; if (path == null) { throw new IllegalStateException("Unable to determine code source archive"); } else { File root = new File(path); if (!root.exists()) { throw new IllegalStateException("Unable to determine code source archive from " + root); } else { return (Archive)(root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root)); } } } } public class MainMethodRunner { private final String mainClassName; private final String[] args; public MainMethodRunner(String mainClass, String[] args) { this.mainClassName = mainClass; this.args = args != null ? (String[])args.clone() : null; } /** * 最終執行的方法,可以發現是利用的反射調用的我們應用程式的主函數 */ public void run() throws Exception { Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName); Method mainMethod = mainClass.getDeclaredMethod("main", String[].class); mainMethod.invoke((Object)null, this.args); } }
小結:
內容太多了,未涉及歸檔文件,協議處理器,打包war同樣的可以用命令啟動等,感興趣的讀者請親自去調試一番,添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-loader</artifactId> </dependency>
IDEA進行啟動類的配置