SpringBoot-核心介绍
# SpringBoot 源码解析(一)—— SpringBoot核心原理入门
# Spring Boot 概述
Build Anything with Spring Boot
Spring Boot is the starting point for building all Spring-based applications.
Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.
这段话出自 Spring 官网,大意是:
Spring Boot 是所有基于 Spring 开发的项目的起点。它的设计目标是让你尽可能快地跑起来 Spring 应用程序,并尽可能减少配置。
# 什么是 Spring Boot
Spring Boot 是 Spring 框架的一个子项目,它主要用于简化 Spring 应用的开发,提供了一整套开箱即用的功能,比如:
- 内嵌 Web 服务器(如 Tomcat、Jetty、Undertow)
- 自动配置(AutoConfiguration)
- 约定优于配置(Convention over Configuration,框架默认帮你做好合理选择,你只要按它建议的方式写代码,就不用写额外配置。)
- 无 XML 配置(几乎不需要
applicationContext.xml) - Starter 依赖管理(简化
pom.xml)
一句话总结:Spring Boot 让 Spring 开发更简单、更快速,避免大量的 XML 配置,开箱即用!
# Spring 和 Spring Boot 的区别
| 特性 | Spring | Spring Boot |
|---|---|---|
| 需要的配置 | 需要手动配置(XML/Java) | 约定优于配置,自动装配 |
| Web 服务器 | 需要手动配置 Tomcat、Jetty | 内嵌 Tomcat,开箱即用 |
| 依赖管理 | 依赖版本需要手动维护 | Starter 依赖自动管理 |
| 开发效率 | 配置复杂,开发慢 | 简单易用,开发快 |
| 微服务支持 | 需要自己搭建 | 天然支持微服务 |
# 快速启动一个SpringBoot项目
# 1. 导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.chenhao</groupId>
<artifactId>springboot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 2. 编写主程序
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个 Spring Boot 应用
*/
@SpringBootApplication
public class SpringDemoApplication {
public static void main(String[] args) {
// Spring 应用启动
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
# 3. 编写 Controller、Service
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello world";
}
}
2
3
4
5
6
7
8
# 4. 运行主程序测试
打包成 jar 包后运行:
java -jar xxx.jar
访问 http://localhost:8080/hello,即可看到输出:
Hello world
# SpringBoot 启动原理分析
# 1. POM 文件
父项目配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/>
</parent>
2
3
4
5
6
其父项目为:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
2
3
4
5
6
👉 该父项目是 Spring Boot 的 依赖版本仲裁中心,所以我们在导入依赖时通常不需要写版本号。
# 2. 启动器(Starter)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2
3
4
spring-boot-starter 是 Spring Boot 的“场景启动器”,可以帮助我们一次性导入某一类应用场景所需的所有依赖。
例如 spring-boot-starter-web 就是一个 Web 场景启动器,它内部包含了:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
2
3
4
5
6
7
8
9
10
11
12
加入一个 Starter,就相当于同时引入了多个常用依赖组件, Spring Boot 会自动为这些组件进行合理配置。
# 3. 主程序类(主入口类)
@SpringBootApplication
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
2
3
4
5
6
7
@SpringBootApplication 是 Spring Boot 的核心注解之一,它标识当前类是一个 Spring Boot 应用的启动类,
并触发自动配置与组件扫描。
# 4. @SpringBootApplication 注解分析
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
})
public @interface SpringBootApplication {}
2
3
4
5
6
7
8
可以看到,这个注解实际上是三个注解的组合:
| 注解 | 作用 | 说明 |
|---|---|---|
@SpringBootConfiguration | 声明这是一个 Spring Boot 的配置类 | 本质上是 @Configuration 的派生注解 |
@EnableAutoConfiguration | 开启自动配置功能 | 是 Spring Boot 自动装配的核心 |
@ComponentScan | 自动扫描并注册组件 | 默认扫描主类所在包及其子包 |
# @SpringBootConfiguration
@Configuration
public @interface SpringBootConfiguration {}
2
可以看出,它其实就是 @Configuration 的别名。意味着主类本身是一个 Spring 的配置类,可以注册 Bean、导入配置等。
# @ComponentScan
@ComponentScan(
excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
})
2
3
4
5
默认情况下,Spring Boot 会从启动类所在的包开始,递归扫描所有子包中的组件(@Controller、@Service、@Repository、@Component 等),并将它们注册进容器。
# @EnableAutoConfiguration
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
2
3
可以看到它做了两件重要的事:
- 注册自动配置包(
@AutoConfigurationPackage)- 默认会将启动类所在包作为基础包,导入到自动配置体系中,方便后续自动配置类在加载时进行包扫描。
- 导入自动配置选择器(
@Import(EnableAutoConfigurationImportSelector.class))- 该选择器会读取所有
META-INF/spring.factories中声明的自动配置类,并按条件装配。
- 该选择器会读取所有
# 5. Spring Boot 启动流程
当调用 SpringApplication.run(...) 时,Spring Boot 会执行一系列自动配置:
- 创建
SpringApplication对象 - 自动推断应用类型(Servlet / Reactive / None)
- 加载
ApplicationContext - 执行
@SpringBootApplication扫描并加载 Bean - 加载
spring.factories进行自动配置 - 启动 Web 服务器(Tomcat/Jetty)
- 初始化
CommandLineRunner/ApplicationRunner