从01开始 从01开始
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)

peterjxl

人生如逆旅,我亦是行人
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)
  • 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完整启动过程
      • SpringBoot
  • Java并发

  • Java源码

  • JVM

  • 韩顺平

  • Java
  • 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,读者可以通过切换分支来查看本文的示例代码

在GitHub上编辑此页 (opens new window)
上次更新: 2023/8/23 10:10:57
HelloWorld
底层注解-@Configuration详解

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

Theme by Vdoing | Copyright © 2022-2023 粤ICP备2022067627号-1 粤公网安备 44011302003646号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式