溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot為什么可以使用Jar包啟動

發(fā)布時間:2022-03-23 14:06:31 來源:億速云 閱讀:162 作者:小新 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關SpringBoot為什么可以使用Jar包啟動,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

    引言

    很多初學者會比較困惑,Spring Boot 是如何做到將應用代碼和所有的依賴打包成一個獨立的 Jar 包,因為傳統(tǒng)的 Java 項目打包成 Jar 包之后,需要通過 -classpath 屬性來指定依賴,才能夠運行。

    Spring Boot 打包插件

    Spring Boot 提供了一個名叫 spring-boot-maven-plugin 的 maven 項目打包插件,如下:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

    可以方便的將 Spring Boot 項目打成 jar 包。 這樣我們就不再需要部署 Tomcat 、Jetty等之類的 Web 服務器容器啦。

    我們先看一下 Spring Boot 打包后的結構是什么樣的,打開 target 目錄我們發(fā)現(xiàn)有兩個jar包:

    SpringBoot為什么可以使用Jar包啟動

    其中,springboot-0.0.1-SNAPSHOT.jar 是通過 Spring Boot 提供的打包插件采用新的格式打成 Fat Jar,包含了所有的依賴;

    springboot-0.0.1-SNAPSHOT.jar.original 則是Java原生的打包方式生成的,僅僅只包含了項目本身的內容。

    SpringBoot FatJar 的組織結構

    我們將 Spring Boot 打的可執(zhí)行 Jar 展開后的結構如下所示:

    SpringBoot為什么可以使用Jar包啟動

    • BOOT-INF目錄:包含了我們的項目代碼(classes目錄),以及所需要的依賴(lib 目錄);

    • META-INF目錄:通過 MANIFEST.MF 文件提供 Jar包的元數(shù)據(jù),聲明了 jar 的啟動類;

    • org.springframework.boot.loader :Spring Boot 的加載器代碼,實現(xiàn)的 Jar in Jar 加載的魔法源。

    我們看到,如果去掉BOOT-INF目錄,這將是一個非常普通且標準的Jar包,包括元信息以及可執(zhí)行的代碼部分,其/META-INF/MAINFEST.MF指定了Jar包的啟動元信息,org.springframework.boot.loader 執(zhí)行對應的邏輯操作。

    MAINFEST.MF 元信息

    元信息內容如下所示:

    Manifest-Version: 1.0
    Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
    Implementation-Title: springboot
    Implementation-Version: 0.0.1-SNAPSHOT
    Spring-Boot-Layers-Index: BOOT-INF/layers.idx
    Start-Class: com.listenvision.SpringbootApplication
    Spring-Boot-Classes: BOOT-INF/classes/
    Spring-Boot-Lib: BOOT-INF/lib/
    Build-Jdk-Spec: 1.8
    Spring-Boot-Version: 2.5.6
    Created-By: Maven Jar Plugin 3.2.0
    Main-Class: org.springframework.boot.loader.JarLauncher

    它相當于一個 Properties 配置文件,每一行都是一個配置項目。重點來看看兩個配置項:

    • Main-Class 配置項:Java 規(guī)定的 jar 包的啟動類,這里設置為 spring-boot-loader 項目的 JarLauncher 類,進行 Spring Boot 應用的啟動。

    • Start-Class 配置項:Spring Boot 規(guī)定的主啟動類,這里設置為我們定義的 Application 類。

    • Spring-Boot-Classes 配置項:指定加載應用類的入口。

    • Spring-Boot-Lib 配置項: 指定加載應用依賴的庫。

    啟動原理

    Spring Boot 的啟動原理如下圖所示:

    SpringBoot為什么可以使用Jar包啟動

    源碼分析

    JarLauncher

    JarLauncher 類是針對 Spring Boot jar 包的啟動類, 完整的類圖如下所示:

    SpringBoot為什么可以使用Jar包啟動

    其中的 WarLauncher 類,是針對 Spring Boot war 包的啟動類。 啟動類 org.springframework.boot.loader.JarLauncher 并非為項目中引入類,而是 spring-boot-maven-plugin 插件 repackage 追加進去的。

    接下來我們先來看一下 JarLauncher 的源碼,比較簡單,如下圖所示:

    public class JarLauncher extends ExecutableArchiveLauncher {
        private static final String DEFAULT_CLASSPATH_INDEX_LOCATION = "BOOT-INF/classpath.idx";
        static final EntryFilter NESTED_ARCHIVE_ENTRY_FILTER = (entry) -> {
            if (entry.isDirectory()) {
                return entry.getName().equals("BOOT-INF/classes/");
            }
            return entry.getName().startsWith("BOOT-INF/lib/");
        };
        
        public JarLauncher() {
        }
        
        protected JarLauncher(Archive archive) {
            super(archive);
        }
        
        @Override
        protected ClassPathIndexFile getClassPathIndex(Archive archive) throws IOException {
            // Only needed for exploded archives, regular ones already have a defined order
            if (archive instanceof ExplodedArchive) {
                String location = getClassPathIndexFileLocation(archive);
                return ClassPathIndexFile.loadIfPossible(archive.getUrl(), location);
            }
            return super.getClassPathIndex(archive);
        }
        
       
        private String getClassPathIndexFileLocation(Archive archive) throws IOException {
            Manifest manifest = archive.getManifest();
            Attributes attributes = (manifest != null) ? manifest.getMainAttributes() : null;
            String location = (attributes != null) ? attributes.getValue(BOOT_CLASSPATH_INDEX_ATTRIBUTE) : null;
            return (location != null) ? location : DEFAULT_CLASSPATH_INDEX_LOCATION;
        }
        
        @Override
        protected boolean isPostProcessingClassPathArchives() {
            return false;
        }
        
        @Override
        protected boolean isSearchCandidate(Archive.Entry entry) {
            return entry.getName().startsWith("BOOT-INF/");
        }
        
        @Override
        protected boolean isNestedArchive(Archive.Entry entry) {
            return NESTED_ARCHIVE_ENTRY_FILTER.matches(entry);
        }
        
        public static void main(String[] args) throws Exception {
            //調用基類 Launcher 定義的 launch 方法
            new JarLauncher().launch(args);
        }
    }

    主要看它的 main 方法,調用的是基類 Launcher 定義的 launch 方法,而 Launcher 是ExecutableArchiveLauncher 的父類。下面我們來看看Launcher基類源碼:

    Launcher

    public abstract class Launcher {
        private static final String JAR_MODE_LAUNCHER = "org.springframework.boot.loader.jarmode.JarModeLauncher";
        protected void launch(String[] args) throws Exception {
            if (!isExploded()) {
                JarFile.registerUrlProtocolHandler();
            }
            ClassLoader classLoader = createClassLoader(getClassPathArchivesIterator());
            String jarMode = System.getProperty("jarmode");
            String launchClass = (jarMode != null && !jarMode.isEmpty()) ? JAR_MODE_LAUNCHER : getMainClass();
            launch(args, launchClass, classLoader);
        }
        
        @Deprecated
        protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
            return createClassLoader(archives.iterator());
        }
        
        protected ClassLoader createClassLoader(Iterator<Archive> archives) throws Exception {
            List<URL> urls = new ArrayList<>(50);
            while (archives.hasNext()) {
                urls.add(archives.next().getUrl());
            }
            return createClassLoader(urls.toArray(new URL[0]));
        }
        
        protected ClassLoader createClassLoader(URL[] urls) throws Exception {
            return new LaunchedURLClassLoader(isExploded(), getArchive(), urls, getClass().getClassLoader());
        }
        
        protected void launch(String[] args, String launchClass, ClassLoader classLoader) throws Exception {
            Thread.currentThread().setContextClassLoader(classLoader);
            createMainMethodRunner(launchClass, args, classLoader).run();
        }
        
        protected MainMethodRunner createMainMethodRunner(String mainClass, String[] args, ClassLoader classLoader) {
            return new MainMethodRunner(mainClass, args);
        }
        protected abstract String getMainClass() throws Exception;
        
        protected Iterator<Archive> getClassPathArchivesIterator() throws Exception {
            return getClassPathArchives().iterator();
        }
        
        @Deprecated
        protected List<Archive> getClassPathArchives() throws Exception {
            throw new IllegalStateException("Unexpected call to getClassPathArchives()");
        }
        
        protected final Archive createArchive() throws Exception {
            ProtectionDomain protectionDomain = 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");
            }
            File root = new File(path);
            if (!root.exists()) {
                throw new IllegalStateException("Unable to determine code source archive from " + root);
            }
            return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
        }
        
        protected boolean isExploded() {
            return false;
        }
        
        protected Archive getArchive() {
            return null;
        }
    }
    • launch 方法會首先創(chuàng)建類加載器,而后判斷是否 jar 是否在 MANIFEST.MF 文件中設置了 jarmode 屬性。

    • 如果沒有設置,launchClass 的值就來自 getMainClass() 返回,該方法由PropertiesLauncher子類實現(xiàn),返回 MANIFEST.MF 中配置的 Start-Class 屬性值。

    • 調用 createMainMethodRunner 方法,構建一個 MainMethodRunner 對象并調用其 run 方法。

    PropertiesLauncher

    @Override
    protected String getMainClass() throws Exception {
        //加載 jar包 target目錄下的  MANIFEST.MF 文件中 Start-Class配置,找到springboot的啟動類
        String mainClass = getProperty(MAIN, "Start-Class");
        if (mainClass == null) {
            throw new IllegalStateException("No '" + MAIN + "' or 'Start-Class' specified");
        }
        return mainClass;
    }

    MainMethodRunner

    目標類main方法的執(zhí)行器,此時的 mainClassName 被賦值為 MANIFEST.MF 中配置的 Start-Class 屬性值,也就是 com.listenvision.SpringbootApplication,之后便是通過反射執(zhí)行 SpringbootApplication 的 main 方法,從而達到啟動 Spring Boot 的效果。

    public class MainMethodRunner {
        private final String mainClassName;
        private final String[] args;
        public MainMethodRunner(String mainClass, String[] args) {
            this.mainClassName = mainClass;
            this.args = (args != null) ? args.clone() : null;
        }
        public void run() throws Exception {
            Class<?> mainClass = Class.forName(this.mainClassName, false, Thread.currentThread().getContextClassLoader());
            Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
            mainMethod.setAccessible(true);
            mainMethod.invoke(null, new Object[] { this.args });
        }
    }

    關于“SpringBoot為什么可以使用Jar包啟動”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

    AI