1.本地打好一个基于springboot项目的jar包
springboot-demo-0.0.1-SNAPSHOT.jar
2.解压jar包查看目录结构
2.1 BOOT-INF下目录
BOOT-INF分为两部分,classes为整个项目的class文件
lib为项目依赖的所有jar包
2.2 META-INF下目录
其中maven下为pom的管理信息,
其中pom.xml就是项目中的源文件,
pom.properties为基础信息
2.3 MANIFEST.MF(核心)
MANIFEST.MF作为springboot的核心配置文件,里面记录了各种版本信息和lib库,jdk等。
例如:Manifest-Version,项目名,自定义pom-version,启动类main方法,完整源文件,lib库,运行环境jdk版本,springboot版本,主类加载器
Manifest-Version: 1.0
Implementation-Title: springboot-demo
Implementation-Version: 0.0.1-SNAPSHOT
Start-Class: com.sjzl.SpringbootDemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.1.11.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher
基于这些信息,不难看出,springboot项目是如何启动的,通过main-class加载器,加载start-class,通过main方法里面的组合注解读取所有配置并加入到pring容器中。
2.4 org包下即为springboot类加载器
核心为JarLauncher,通过BOOT_INF_CLASSES和BOOT_INF_LIB可以读取项目编译后的class文件和所有依赖的jar(里面编译后的文件)
static final String BOOT_INF_CLASSES = "BOOT-INF/classes/";
static final String BOOT_INF_LIB = "BOOT-INF/lib/";
JarLauncher完整源代码:
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(org.springframework.boot.loader.archive.Archive.Entry entry)
{
if (entry.isDirectory())
return entry.getName().equals("BOOT-INF/classes/");
else
return entry.getName().startsWith("BOOT-INF/lib/");
}
public static void main(String args[])
throws Exception
{
(new JarLauncher()).launch(args);
}
}
JarLauncher extends ExecutableArchiveLauncher
ExecutableArchiveLauncher extends Launcher