Springboot启动流程剖析

一、前言

Spring Framework的强大扩展点,赋予了对象丰富的生命周期;但是其配置繁琐,而且不太灵活,什么都需要自己来配置引入;简直就是优秀的架构,构造的噩梦。就在这过程中,官方Springboot以快速构建突破了这一痛点,也被开发者赋予Spring急行军的称号。通过下面3点我们将了解简单的Springboot:

本章不会涉及到springboot的自动装配,仅针对启动流程!!!

1.Springboot项目搭建
2.游走Springboot启动流程源码
3.Springboot启动流程图

二、Springboot急行军项目搭建

启动入口

1
2
3
4
5
6
@SpringBootApplication
public class SpringApplication {
public static void main(String[] args) {
org.springframework.boot.SpringApplication.run(SpringApplication.class,args);
}
}

依赖
maven

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>

gradle

1
2
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter
implementation group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.3.5.RELEASE'

执行输出结果

1
2
3
4
5
6
7
8
9
10
11
12

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.5.RELEASE)

2021-06-23 13:44:14.090 INFO [main] c.x.c.SpringApplication - [logStarting,55] - Starting SpringApplication on localhost with PID 49608 (/Users/wangcai/appData/development/projects/java/self/springboothome/cache/build/classes/java/main started by wangcai in /Users/wangcai/appData/development/projects/java/self/springboothome)
2021-06-23 13:44:14.096 INFO [main] c.x.c.SpringApplication - [logStartupProfileInfo,651] - No active profile set, falling back to default profiles: default
2021-06-23 13:44:14.646 INFO [main] c.x.c.SpringApplication - [logStarted,61] - Started SpringApplication in 0.832 seconds (JVM running for 1.884)

三、游走Springboot启动流程源码

简单了解@SpringBootApplication
@SpringBootApplication

完成扫描和启动自动装配

avatar
深入理解Springboot启动流程
SpringApplication
avatar
avatar

最终会进入主构造

avatar

真正的启动流程(只会针对重要的来进行说明)

avatar
1⃣️、创建合适的应用上下文
avatar
avatar
2⃣️、准备上下文

监听上下文、注册实例(run入口传入的类)

avatar
3⃣️、刷新上下文
avatar
avatar

四、Springboot启动流程图

avatar

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信