springboot source code analysis - jar startup

overview

Spring Boot provides the Maven plug-in spring-boot-maven-plugin, which can easily package Spring Boot projects into jar packages or war packages.

How SpringBoot is started through the jar package

What does java -jar do? See what the official website says

If the -jar option is specified, its argument is the name of the JAR file containing class and resource files for the application. The startup class must be indicated by the Main-Class manifest header in its source code.

Next, let's open a Spring Boot jar package and see its structure
insert image description here

① META-INF directory: The metadata of the jar package is provided through the MANIFEST.MF file, and the startup class of the jar is declared.
② org directory: The spring-boot-loader project provided for Spring Boot, it is the secret of java -jar to start the Spring Boot project, and it is also the part we will learn more about later.
③ BOOT-INF/lib directory: The dependent jar packages introduced in our Spring Boot project. One of the great functions of the spring-boot-loader project is to solve the situation of nesting jars in the jar package and how to load the classes into it.
④ BOOT-INF/classes directory: The .class, configuration files, etc. compiled by the Java class in the Spring Boot project.

MANIFEST.MF

Let's look at the META-INF/MANIFEST.MF file, the content of which is as follows:

Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: ahs-account
Implementation-Version: 0.0.1-SNAPSHOT
Start-Class: com.ahs.ahsaccount.AhsAccountApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.3.2.RELEASE
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher

explain

Main-Class configuration item: The startup class of the jar package specified by Java. Here, it is set to the JarLauncher class of the spring-boot-loader project to start the Spring Boot application.
Start-Class configuration item: The main startup class specified by Spring Boot, here is set to the Application class we defined.

Why there are Main-Class and Start-Class

Java stipulates that the jar package of the executable cannot be nested with other jar packages. But we can see that under the BOOT-INF/lib directory, there are actually all jar packages that the Spring Boot application depends on. Therefore, the spring-boot-loader project custom implements the ClassLoader implementation class LaunchedURLClassLoader, which supports loading .class files in the BOOT-INF/classes directory and jar packages in the BOOT-INF/lib directory.

In fact,
java -jar will find the manifest file in the jar, and find the real startup class in it;

Summarize

After springboot is packaged by the mvn packaging plug-in, after executing the java -jar command, it will execute the mainclass of the MANIFEST.MF file and load the nested jar package in the lib directory and the files in the calss directory, and finally find the real startup class of the springboot project. It is the startup class corresponding to startcalss

Guess you like

Origin blog.csdn.net/qq_42600094/article/details/131075474