Yang's blog Yang's blog
首页
后端开发
密码学
机器学习
命令手册
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

xiaoyang

尽人事,听天命
首页
后端开发
密码学
机器学习
命令手册
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • SpringCloud

    • 微服务架构介绍
    • SpringCloud介绍
    • Spring Cloud:生产者与消费者
    • Spring Cloud Eureka:构建可靠的服务注册与发现
    • Spring Cloud Ribbon:负载均衡
    • Spring Cloud Fegin:服务调用
    • Spring Cloud Hystrix:熔断器
    • Spring Cloud Zuul:统一网关路由
    • Spring Cloud Config:配置中心
  • Java后端框架

    • LangChain4j

      • 介绍
      • 快速开始
      • Chat and Language Models
      • Chat Memory
      • Model Parameters
      • Response Streaming
      • AI Services
      • Agent
      • Tools (Function Calling)
      • RAG
      • Structured Outputs
      • Classification
      • Embedding (Vector) Stores
      • Image Models
      • Quarkus Integration
      • Spring Boot Integration
      • Kotlin Support
      • Logging
      • Observability
      • Testing and Evaluation
      • Model Context Protocol
    • SpringBoot

      • SpringBoot-核心介绍
        • Spring Boot 概述
        • 什么是 Spring Boot
        • Spring 和 Spring Boot 的区别
        • 快速启动一个SpringBoot项目
          • 1. 导入依赖
          • 2. 编写主程序
          • 3. 编写 Controller、Service
          • 4. 运行主程序测试
        • SpringBoot 启动原理分析
          • 1. POM 文件
          • 2. 启动器(Starter)
          • 3. 主程序类(主入口类)
          • 4. @SpringBootApplication 注解分析
          • @SpringBootConfiguration
          • @ComponentScan
          • @EnableAutoConfiguration
          • 5. Spring Boot 启动流程
  • 八股文

    • 操作系统
    • JVM介绍
    • Java多线程
    • Java集合框架
    • Java反射
    • JavaIO
    • Mybatis介绍
    • Spring介绍
    • SpringBoot介绍
    • Mysql
    • Redis
    • 数据结构
    • 云计算
    • 设计模式
    • 计算机网络
    • 锁核心类AQS
    • Nginx
    • 面试场景题
  • 前端技术

    • 初识Vue3
    • Vue3数据双向绑定
    • Vue3生命周期
    • Vue-Router 组件
    • Pinia 集中式状态存储
  • 中间件

    • RocketMQ
  • 源码分析

    • 线程池源码解析
    • Redisson源码分析
  • 开发知识

    • 请求参数注解
    • 时间复杂度和空间复杂度
    • JSON序列化与反序列化
    • Timestamp vs Datetime
    • Java开发中必备能力单元测试
    • 正向代理和反向代理
    • 什么是VPN
    • 后端服务端主动推送消息的常见方式
    • 正则表达式
    • SseEmitter vs Flux 的本质区别与底层原理解析
    • Function Calling与MCP区别
    • 分布式事务
  • 后端开发
  • Java后端框架
  • SpringBoot
xiaoyang
2025-11-13
目录

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>
1
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);
    }
}
1
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";
    }
}
1
2
3
4
5
6
7
8

# 4. 运行主程序测试

打包成 jar 包后运行:

java -jar xxx.jar
1

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

Hello world
1

# SpringBoot 启动原理分析

# 1. POM 文件

父项目配置如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.9.RELEASE</version>
    <relativePath/>
</parent>
1
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>
1
2
3
4
5
6

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


# 2. 启动器(Starter)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
1
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>
1
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);
    }
}
1
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 {}
1
2
3
4
5
6
7
8

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

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

# @SpringBootConfiguration

@Configuration
public @interface SpringBootConfiguration {}
1
2

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

# @ComponentScan

@ComponentScan(
    excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
    })
1
2
3
4
5

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

# @EnableAutoConfiguration

@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
1
2
3

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

  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
编辑 (opens new window)
上次更新: 2025/11/20, 02:25:22

← Model Context Protocol 操作系统→

最近更新
01
Redisson源码分析
11-13
02
线程池源码解析
10-31
03
分布式事务
10-27
更多文章>
Theme by Vdoing | Copyright © 2023-2025 xiaoyang | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式