通过IDEA 自动构建一个SpringBoot项目,通过maven构建工具进行打包之后,在target目录下,生成Jar包,使用java -jar target.jar,一个简单的SpringBoot项目即可启动完成。
Jar包启动的逻辑:
Jar包下必有META-INFO/MANIFEST.MF 文件,这个文件定义了构建时间,构建人,构建的工具版本,构建的JDK版本等信息
如:servlet-api.jar下的配置文件
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: Apache Maven
Built-By: edburns
Package:
Build-Jdk: 1.5.0_06
Extension-Name: servlet-api
Specification-Title: A component of the Glassfish Application Server
Specification-Vendor: Sun Microsystems Inc
Implementation-Title:
Implementation-Vendor: Sun Microsystems Inc
Implementation-Version: 2.5
一个Jar包启动的时候,启动主函数通过配置MANIFEST.MF 中的 Main-Class来决定,springboot项目也不例外
SpringBoot项目的MANIFEST.MF 如下
Manifest-Version: 1.0
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: zhaokui
Implementation-Vendor-Id: com.example
Spring-Boot-Version: 2.0.5.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.example.demo.DemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.4
Build-Jdk: 1.8.0_181
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
ot-starter-parent/demo
由此可以看出SpringBoot的启动类并非我们的 com.example.demo.DemoApplication,而是 org.springframework.boot.loader.JarLauncher。
SpringBoot通过指定我们的运行的Classes路径和Lib路径来加载我们的类,然后使用反射invoke我们的主函数来加载进行启动
所以springBootJar包项目的目录结构如下
drwxr-xr-x 4 hello staff 128B 9 25 21:26 BOOT-INF
drwxr-xr-x 4 hello staff 128B 9 25 21:26 META-INF
-rw-r--r-- 1 hello staff 16M 9 25 21:26 demo-0.0.1-SNAPSHOT.jar
drwxr-xr-x 3 hello staff 96B 9 25 21:26 org
其中org目录下JarLaunher的启动类,BOOT-IFNO里面包含了项目依赖的jar包以及我们编写的代码
因此 SpringBoot项目 只需要简单的 java -jar 项目.jar即可,所有的配置MANIFEST.MF,在Maven springboot plugin中自动生成配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
后续会带来Spring配置文件如何自动加载,以及加载顺序和加载逻辑