Skip to content
xiaoyang 的头像xiaoyang发布于 2025-11-135,643 字阅读约 19 分钟浏览加载中

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 的区别 ​

特性SpringSpring Boot
需要的配置需要手动配置(XML/Java)约定优于配置,自动装配
Web 服务器需要手动配置 Tomcat、Jetty内嵌 Tomcat,开箱即用
依赖管理依赖版本需要手动维护Starter 依赖自动管理
开发效率配置复杂,开发慢简单易用,开发快
微服务支持需要自己搭建天然支持微服务

快速启动一个SpringBoot项目 ​

1. 导入依赖 ​

xml
<?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. 编写主程序 ​

java
/**
 * @SpringBootApplication 来标注一个主程序类,说明这是一个 Spring Boot 应用
 */
@SpringBootApplication
public class SpringDemoApplication {

    public static void main(String[] args) {
        // Spring 应用启动
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

3. 编写 Controller、Service ​

java
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello world";
    }
}

4. 运行主程序测试 ​

打包成 jar 包后运行:

bash
java -jar xxx.jar

访问 http://localhost:8080/hello,即可看到输出:

Hello world

SpringBoot 启动原理分析 ​

1. POM 文件 ​

父项目配置如下:

xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.9.RELEASE</version>
    <relativePath/>
</parent>

其父项目为:

xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.9.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

👉 该父项目是 Spring Boot 的 依赖版本仲裁中心,所以我们在导入依赖时通常不需要写版本号。


2. 启动器(Starter) ​

xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter 是 Spring Boot 的“场景启动器”,可以帮助我们一次性导入某一类应用场景所需的所有依赖。

例如 spring-boot-starter-web 就是一个 Web 场景启动器,它内部包含了:

xml
<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>

加入一个 Starter,就相当于同时引入了多个常用依赖组件, Spring Boot 会自动为这些组件进行合理配置。


3. 主程序类(主入口类) ​

java
@SpringBootApplication
public class SpringDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

@SpringBootApplication 是 Spring Boot 的核心注解之一,它标识当前类是一个 Spring Boot 应用的启动类, 并触发自动配置与组件扫描。


4. @SpringBootApplication 注解分析 ​

java
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
    })
public @interface SpringBootApplication {}

可以看到,这个注解实际上是三个注解的组合:

注解作用说明
@SpringBootConfiguration声明这是一个 Spring Boot 的配置类本质上是 @Configuration 的派生注解
@EnableAutoConfiguration开启自动配置功能是 Spring Boot 自动装配的核心
@ComponentScan自动扫描并注册组件默认扫描主类所在包及其子包

@SpringBootConfiguration ​

java
@Configuration
public @interface SpringBootConfiguration {}

可以看出,它其实就是 @Configuration 的别名。意味着主类本身是一个 Spring 的配置类,可以注册 Bean、导入配置等。

@ComponentScan ​

java
@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
    })

默认情况下,Spring Boot 会从启动类所在的包开始,递归扫描所有子包中的组件(@Controller、@Service、@Repository、@Component 等),并将它们注册进容器。

@EnableAutoConfiguration ​

java
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}

可以看到它做了两件重要的事:

  1. 注册自动配置包(@AutoConfigurationPackage)
    • 默认会将启动类所在包作为基础包,导入到自动配置体系中,方便后续自动配置类在加载时进行包扫描。
  2. 导入自动配置选择器(@Import(EnableAutoConfigurationImportSelector.class))
    • 该选择器会读取所有 META-INF/spring.factories 中声明的自动配置类,并按条件装配。

5. Spring Boot 启动流程 ​

当调用 SpringApplication.run(...) 时,Spring Boot 会执行一系列自动配置:

  1. 创建 SpringApplication 对象
  2. 自动推断应用类型(Servlet / Reactive / None)
  3. 加载 ApplicationContext
  4. 执行 @SpringBootApplication 扫描并加载 Bean
  5. 加载 spring.factories 进行自动配置
  6. 启动 Web 服务器(Tomcat/Jetty)
  7. 初始化 CommandLineRunner / ApplicationRunner
DISCUSSION

评论区

欢迎分享你的思考、补充或问题。

基于 VitePress 搭建 · 聚焦技术深度与极致体验