从 01 开始 从 01 开始
首页
  • 📚 计算机基础

    • 计算机简史
    • 数字电路
    • 计算机组成原理
    • 操作系统
    • Linux
    • 计算机网络
    • 数据库
    • 编程工具
    • 装机
  • 🎨 前端

    • Node
  • JavaSE
  • Java 高级
  • JavaEE

    • 构建、依赖管理
    • Ant
    • Maven
    • 日志框架
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • 环境管理和配置管理-科普篇
    • Servlet
  • Spring

    • Spring基础
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC

    • SpringMVC 基础
  • SpringBoot

    • SpringBoot 基础
  • Windows 使用技巧
  • 手机相关技巧
  • 最全面的输入法教程
  • 最全面的浏览器教程
  • Office
  • 图片类工具
  • 效率类工具
  • 最全面的 RSS 教程
  • 码字工具
  • 各大平台
  • 校招
  • 五险一金
  • 职场规划
  • 关于离职
  • 杂谈
  • 自媒体
  • 📖 读书

    • 读书工具
    • 走进科学
  • 🌍 英语

    • 从零开始学英语
    • 英语兔的相关视频
    • Larry 想做技术大佬的相关视频
  • 🏛️ 政治

    • 反腐
    • GFW
    • 404 内容
    • 审查与自我审查
    • 互联网
    • 战争
    • 读书笔记
  • 💰 经济

    • 关于税
    • 理财
  • 💪 健身

    • 睡眠
    • 皮肤
    • 口腔健康
    • 学会呼吸
    • 健身日志
  • 🏠 其他

    • 驾驶技能
    • 租房与买房
    • 厨艺
  • 电影

    • 电影推荐
  • 电视剧
  • 漫画

    • 漫画软件
    • 漫画推荐
  • 游戏

    • Steam
    • 三国杀
    • 求生之路
  • 小说
  • 关于本站
  • 关于博主
  • 打赏
  • 网站动态
  • 友人帐
  • 从零开始搭建博客
  • 搭建邮件服务器
  • 本站分享
  • 🌈 生活

    • 2022
    • 2023
    • 2024
    • 2025
  • 📇 文章索引

    • 文章分类
    • 文章归档

晓林

程序猿,自由职业者,博主,英语爱好者,健身达人
首页
  • 📚 计算机基础

    • 计算机简史
    • 数字电路
    • 计算机组成原理
    • 操作系统
    • Linux
    • 计算机网络
    • 数据库
    • 编程工具
    • 装机
  • 🎨 前端

    • Node
  • JavaSE
  • Java 高级
  • JavaEE

    • 构建、依赖管理
    • Ant
    • Maven
    • 日志框架
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • 环境管理和配置管理-科普篇
    • Servlet
  • Spring

    • Spring基础
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC

    • SpringMVC 基础
  • SpringBoot

    • SpringBoot 基础
  • Windows 使用技巧
  • 手机相关技巧
  • 最全面的输入法教程
  • 最全面的浏览器教程
  • Office
  • 图片类工具
  • 效率类工具
  • 最全面的 RSS 教程
  • 码字工具
  • 各大平台
  • 校招
  • 五险一金
  • 职场规划
  • 关于离职
  • 杂谈
  • 自媒体
  • 📖 读书

    • 读书工具
    • 走进科学
  • 🌍 英语

    • 从零开始学英语
    • 英语兔的相关视频
    • Larry 想做技术大佬的相关视频
  • 🏛️ 政治

    • 反腐
    • GFW
    • 404 内容
    • 审查与自我审查
    • 互联网
    • 战争
    • 读书笔记
  • 💰 经济

    • 关于税
    • 理财
  • 💪 健身

    • 睡眠
    • 皮肤
    • 口腔健康
    • 学会呼吸
    • 健身日志
  • 🏠 其他

    • 驾驶技能
    • 租房与买房
    • 厨艺
  • 电影

    • 电影推荐
  • 电视剧
  • 漫画

    • 漫画软件
    • 漫画推荐
  • 游戏

    • Steam
    • 三国杀
    • 求生之路
  • 小说
  • 关于本站
  • 关于博主
  • 打赏
  • 网站动态
  • 友人帐
  • 从零开始搭建博客
  • 搭建邮件服务器
  • 本站分享
  • 🌈 生活

    • 2022
    • 2023
    • 2024
    • 2025
  • 📇 文章索引

    • 文章分类
    • 文章归档
  • JavaSE

  • JavaSenior

  • JavaEE

  • JavaWeb

  • Spring

  • 主流框架

  • SpringMVC

  • SpringBoot

    • SpringBoot 教程-尚硅谷

      • SpringBoot 课程介绍
      • Spring 和 SpringBoot
      • HelloWorld
      • 了解自动配置原理
        • 依赖管理
        • 自动配置
        • 源码
      • 底层注解-@Configuration 详解
      • 底层注解-@Import 导入组件
      • 底层注解-@Conditional 条件装配
      • 原生配置文件引入-@ImportResource
      • 底层注解-配置绑定 @ConfigurationProperties
      • 自动配置原理
      • 自动配置流程
      • Lombok 简化开发
      • DevTools
      • Spring-Initailizr
      • 配置文件-Yaml 用法
      • Web 开发简介
      • web 开发-静态资源规则于定制化
      • 静态资源配置原理
      • Rest 映射及源码解析
      • 请求映射原理
      • 常用参数注解使用
      • MatrixVariable:矩阵变量
      • 各种类型参数解析原理
      • Servlet-API 参数解析原理
      • Model、Map 参数解析原理
      • 自定义对象参数绑定原理
      • 自定义 Converter 原理
      • 数据响应原理
      • 内容协商原理
      • 基于请求参数的内容原理
      • 自定义 MessageConverter 原理
      • Thymeleaf 初体验
      • web 实验-后台管理系统
      • web 实验-抽取公共页面
      • web 实验-遍历数据
      • 源码分析-视图解析器与视图
      • 拦截器-登录检查与静态资源放行
      • 拦截器的执行时机和原理
      • 单文件和多文件上传的使用
      • 文件上传原理
      • 错误处理机制
      • 错误处理-底层组件源码分析
      • 异常处理流程
      • 几种异常处理原理
      • Web 原生对象注入
      • 嵌入式 Servlet 容器
      • 定制化原理
      • 数据库场景的自动配置分析和整合测试
      • 自定义方式整合 Druid
      • 通过 starter 整合 Druid
      • 整合 Mybatis
      • 使用注解整合 Mybatis
      • 整合 MybatisPlus 操作数据库
      • MybatisPlus-列表分页展示
      • 整合 Redis
      • 单元测试-Junit5
      • 单元测试-断言机制
      • 单元测试-前置条件
      • 单元测试-嵌套测试
      • 单元测试-参数化测试
      • 指标监控-基本概念
      • 指标监控-配置 EndPoint
      • 指标监控-可视化
      • 原理解析-Profile 功能
      • 配置文件深入
      • 自定义 Starter
      • SpringApplication 初始化过程
      • SpringBoot 完整启动过程
  • Java
  • SpringBoot
  • SpringBoot 教程-尚硅谷
2023-08-22
目录

了解自动配置原理

# 40.了解自动配置原理

通过上文的介绍,相信大家都知道使用 SpringBoot 是很方便的,这基于 SpringBoot 提供的两大特性:依赖管理,自动配置。 ‍

# 依赖管理

  • 父项目做依赖管理
  • 无需关注版本号,自动版本仲裁。也可以修改版本号
  • 开发导入 starter 场景启动器
  • ....

# 父项目做依赖管理

在 Maven 中,父项目常用做依赖管理。我们只需引入一个 SpringBoot 的父项目,就不用关心版本问题了。后续我们引入 starter,都不用写版本号。 ‍ 我们可以在 IDEA 中按住 ctrl,然后点 pom.xml 文件中的父项目,可以看到父项目的内容:可以看到其还有一个父项目 spring-boot-dependencies

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>
1
2
3
4
5

‍

# 版本号

‍ 我们再点进去,可以看到里面配置了很多 properties:这就是配置了常用框架的版本号,包括 Spring 的,所以这也就是我们不用写版本号的原因。

 <properties>
    <activemq.version>5.15.13</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.82</appengine-sdk.version>
    <artemis.version>2.12.0</artemis.version>
    <aspectj.version>1.9.6</aspectj.version>
    <assertj.version>3.16.1</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <awaitility.version>4.0.3</awaitility.version>
............
1
2
3
4
5
6
7
8
9
10

‍ 一般来说,这些配置好的版本号,也是 SpringBoot 支持的版本号;但开发中,有时候要改版本号(例如漏洞更新),怎么改呢?

  1. 首先查看 spring-boot-dependencies 中配置的版本号是什么,使用的是什么标签
  2. 在项目的 pom.xml 中配置 properties 标签,里面写上版本号即可。

例如使用 MySQL5 的驱动,可以搜到 spring-boot-dependencies 是通过 <mysql.version> 这个标签来配置的,我们也使用该标签来改配置:

<properties>
    <mysql.version>5.1.43</mysql.version>
</properties>
1
2
3

# starter 场景启动器

有时候我们 web 开发,使用 SpringMVC 时,不仅仅要引入 SpringMVC 的框架,还要 Spring 的依赖,日志的依赖等等;

在 SpringBoot 中,将所有 web 开发场景的依赖都整合到了一起,也就是 starter。所以我们上一篇博客的案例,只引入 spring-boot-starter-web,就能将 web 开发所需要的依赖都引入!

在 SpringBoot 中,有很多这样的 starter,官网 (opens new window) 是这样说的:

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

大意:Starters 是一个集合,包含了某个场景的依赖。

一般来说,官方的 starter 都是 spring-boot-starter-* 开头的,* 就代表着某种场景。

只要引入 starter,这个场景的所有常规需要的依赖我们都自动引入。其实原理也很简单,就是该 starter 项目里,引入了一些依赖而已,这是 Maven 的依赖传递。

在文档下方,还列出了所有 starter:几乎所有场景,SpringBoot 都有对应的 starter

Name Description
spring-boot-starter Core starter, including auto-configuration support,
logging and YAML
spring-boot-starter-activemq Starter for JMS messaging using Apache ActiveMQ
spring-boot-starter-amqp Starter for using Spring AMQP and Rabbit MQ
spring-boot-starter-aop Starter for aspect-oriented programming with
Spring AOP and AspectJ
spring-boot-starter-artemis Starter for JMS messaging using Apache Artemis
spring-boot-starter-batch Starter for using Spring Batch
..... .....

如果不满足你的需求,也可以自己写一个 starter:Creating Your Own Starter (opens new window),一般命名为 thirdpartyproject-spring-boot-starter,当然一般用不上。

注意,所有 starter 的父项目都是 spring-boot-starter,这是 SpringBoot 自动配置的核心依赖

# 自动配置

通过上篇博客的案例,我们知道 SpringBoot 帮我们配置了很多东西,例如:

  • 自动配好 Tomcat
  • 自动配好 SpringMVC
  • 自动配好 Web 常见功能(例如字符串编码)
  • 默认的包结构
  • 各种配置都有默认值
  • 按需加载所有自动配置项
  • .....

# 自动配好 Tomcat

以 Tomcat 为例,自动配置,简单来说分为 2 步

  1. 引入 Tomcat 依赖
  2. 配置 Tomcat

例如,spring-boot-starter-web 里,有这样的依赖:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.3.4.RELEASE</version>
      <scope>compile</scope>
</dependency>
1
2
3
4
5
6

至于第二步怎么配置的,我们后续再说 ‍

# 自动配好 SpringMVC

同理,spring-boot-starter-web 里,也有 SpringMVC 的依赖:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.2.9.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.2.9.RELEASE</version>
  <scope>compile</scope>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12

‍ 我们之前使用 SpringMVC,都要配置 dispatcherServlet 和 characterEncodingFilter,那么 SpringBoot 有没配置呢?有的。我们修改下主程序,打印 IoC 容器里的内容:

package com.peterjxl.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 主程序类
 * @SpringBootApplication 这是一个SpringBoot应用程序
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        // 1.返回IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        // 2.查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

然后在打印的内容里,是能搜到 dispatcherServlet 和 characterEncodingFilter 的。 ‍ 我们修改下 controller,试着返回中文:

package com.peterjxl.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController // @RestController = @Controller + @ResponseBody
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "你好, Spring Boot 2!";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

‍ 试着访问:

‍

同理,还配置了 viewResolver 和 multipartResolver 等等组件。 ‍

# 默认的包结构

  • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来,无需以前的包扫描配置
  • 想要改变扫描路径:@SpringBootApplication(scanBasePackages="com.atguigu"),或者用 @ComponentScan 指定扫描路径 ‍
@SpringBootApplication
// 等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.peterjxl.boot")
1
2
3
4
5

‍ 官方文档 (opens new window) 也有说到:

‍

When a class does not include a package declaration, it is considered to be in the “default package”. The use of the “default package” is generally discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the @ComponentScan, @ConfigurationPropertiesScan, @EntityScan, or @SpringBootApplication annotations, since every class from every jar is read.

...............

he following listing shows a typical layout:

com
 +- example
     +- myapplication
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 各种配置都有默认值

举个例子,要配置上传文件的大小:

spring.servlet.multipart.max-file-size=10MB
1

我们可以按住 ctrl 点进该配置项,可以看到其对应一个类:

@ConfigurationProperties(
    prefix = "spring.servlet.multipart",
    ignoreUnknownFields = false
)
public class MultipartProperties {
    private DataSize maxFileSize = DataSize.ofMegabytes(1L);
.....
1
2
3
4
5
6
7

‍ 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象(例如我们刚刚打印的内容中,就有 MultipartProperties 对象):

# 按需加载所有自动配置项

SpringBoot 有很多的 starter,难道全部都会加载配置吗?当然不是,引入了的 starter,该 starter 的自动配置才会开启

每个 starter 都依赖于 spring-boot-starter,而其又依赖于 spring-boot-autoconfigure,(可以通过看 pom.xml 文件分析出),所有自动配置的功能就是由 autoconfigure 来实现的 ‍ 我们可以看这个 jar 包中的内容:

可以看到,这些就是各个 starter 的配置,例如 amqp,cache 等。 ‍

# 源码

已将本文源码上传到 Gitee (opens new window) 或 GitHub (opens new window) 的分支 demo2,读者可以通过切换分支来查看本文的示例代码

上次更新: 2025/6/3 09:31:54
HelloWorld
底层注解-@Configuration 详解

← HelloWorld 底层注解-@Configuration 详解→

最近更新
01
学点统计学:轻松识破一本正经的胡说八道
06-05
02
2025 年 5 月记
05-31
03
《贫穷的本质》很棒,但可能不适合你
05-27
更多文章>
Theme by Vdoing | Copyright © 2022-2025 | 粤 ICP 备 2022067627 号 -1 | 粤公网安备 44011302003646 号 | 点击查看十年之约
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式