SpringBoot深入理解(更新中)

2021-01-03 13:29

阅读:596

标签:etc   nes   static   gradle   possible   match   子类   com   stat   

SpringBoot深入理解

项目打包SpringBoot启动过程

当使用打包时,会下载org-springframework-boot-loader的jar,并且不会放在lib存放的第三方jar包文件中,该jar包中有个JarLauncher.class文件中设置了jar包运行时的入口和打包后文件的结构(定义了BOOT-INF,META-INF中放什么数据)

使用java -jar 启动项目时,因为META-INF中文件记载了启动的顺序

Manifest-Version: 1.0		#版本
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx		
Start-Class: com.zyy.gradletest1.GradleTest1Application	
Spring-Boot-Classes: BOOT-INF/classes/		#记录编译后文件存放地址
Spring-Boot-Lib: BOOT-INF/lib/						  #记录第三方jar包存放地址
Spring-Boot-Version: 2.3.0.RELEASE		        #SpringBoot版本
Main-Class: org.springframework.boot.loader.JarLauncher			#程序的入口

所以程序会直接进入JarLauncher.class中执行main方法

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) -> {
        return entry.isDirectory() ? entry.getName().equals("BOOT-INF/classes/") : entry.getName().startsWith("BOOT-INF/lib/");
    };

    public JarLauncher() {
    }

    protected JarLauncher(Archive archive) {
        super(archive);
    }

    protected ClassPathIndexFile getClassPathIndex(Archive archive) throws IOException {
        if (archive instanceof ExplodedArchive) {
            String location = this.getClassPathIndexFileLocation(archive);
            return ClassPathIndexFile.loadIfPossible(archive.getUrl(), location);
        } else {
            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("Spring-Boot-Classpath-Index") : null;
        return location != null ? location : "BOOT-INF/classpath.idx";
    }

    protected boolean isPostProcessingClassPathArchives() {
        return false;
    }

    protected boolean isSearchCandidate(Entry entry) {
        return entry.getName().startsWith("BOOT-INF/");
    }

    protected boolean isNestedArchive(Entry entry) {
        return NESTED_ARCHIVE_ENTRY_FILTER.matches(entry);
    }

    public static void main(String[] args) throws Exception {
        (new JarLauncher()).launch(args);
    }
}

JarLancher继承了ExecutableArchiveLauncher,ExecutableArchiveLauncher抽象类是归档启动器的父类,它有两个子类

? Lancher:是整个归档的基类(抽象类)

? |

ExecutableArchiveLauncher

? | |

JarLancher WarLancher

顾名思义jarLancher就是打包成jar的归档启动类,WarLancher是War打包方式的归档启动类

SpringBoot深入理解(更新中)

标签:etc   nes   static   gradle   possible   match   子类   com   stat   

原文地址:https://www.cnblogs.com/baroque/p/12990138.html


评论


亲,登录后才可以留言!