Spring Boot源码阅读系列(1)-总览

92 阅读6分钟

源码

在阅读的版本

所阅读的版本为:3.3.x

引言

一些开源项目代码是非常庞大的,如果对每一行代码都进行仔细阅读,不仅效率低下,还容易使人迷失方向。事实上,项目中的不同代码具有不同的重要性。因此,集中精力阅读重要代码是理解项目的关键,能够达到事半功倍的效果。

如何判断是否是核心代码?

  1. 项目通常会有详细的 README 文件,介绍项目的概述、安装步骤和基本用法。文档中特别提到的功能,往往可能是核心功能。
  2. 项目的特点和亮点往往是项目的核心之处。这些特性通常被写在在项目的主页或相关文章。关注这些部分可以快速识别核心功能。
  3. 了解项目issue,issue中多次提到的代码,往往是更需要被关注的代码,这些代码往往涉及到核心功能或关键问题。

如何阅读

  1. 关注项目模块,对项目产生整体印象;通过模块切分项目,降低项目复杂度。并了解模块间的依赖关系。
  2. 确定核心功能,以核心功能代码为主线进行阅读;以此确定阅读方向,避免受非核心代码影响而迷失方向。

特性

  1. 创建独立的 Spring 应用程序 (独立应用)
  2. 直接嵌入 Tomcat、Jetty 或 Undertow(无需部署 WAR 文件) (内嵌Web容器)
  3. 提供 "starter" 依赖关系,以简化构建配置 (起步依赖)
  4. 尽可能自动配置 Spring 和第三方库 (自动配置)
  5. 提供生产就绪功能,如度量、健康检查和外部化配置 (actuator)
  6. 不会生成代码,也不要求 XML 配置 (不产生额外代码与配置)

image.png

模块

来自Spring Boot项目的README文件

image.png

spring-boot

为Springboot其他模块的特性提供支持的主库,主要包括

  • SpringApplication类提供了一个方便的静态方法,来编写一个独立的Spring程序。它的唯一工作是创建和更新一个合适的Spring ApplicationContext
  • 具有可选容器的嵌入式web程序(Tomcat, Jetty, or Undertow)
  • 一流的外部化配置支持
  • 便捷的ApplicationContext初始化器,支持默认的日志配置。

The main library providing features that support the other parts of Spring Boot. These include:

  • The SpringApplication class, providing static convenience methods that can be used to write a stand-alone Spring Application. Its sole job is to create and refresh an appropriate Spring ApplicationContext.
  • Embedded web applications with a choice of container (Tomcat, Jetty, or Undertow).
  • First-class externalized configuration support.
  • Convenience ApplicationContext initializers, including support for sensible logging defaults.

spring-boot-autoconfigure

基于classpath信息(项目依赖信息),Spring Boot可以配置大部分应用。一个@EnableAutoConfiguration注解就会触发Spring上下文的自动配置。

Spring Boot的自动配置会尝试(根据添加的jar)推断用户可能需要的bean。例如,如果HSQLDB在类路径上(被添加为依赖),并且用户没有配置任何数据库连接,那么Spring Boot会定义一个内存数据库。当用户自定义了自己的bean后,会取消自动配置。

Spring Boot can configure large parts of typical applications based on the content of their classpath. A single @EnableAutoConfiguration annotation triggers auto-configuration of the Spring context.

Auto-configuration attempts to deduce which beans a user might need. For example, if HSQLDB is on the classpath, and the user has not configured any database connections, then they probably want an in-memory database to be defined. Auto-configuration will always back away as the user starts to define their own beans.

spring-boot-starters

Starters 是一组依赖。用户可以一次性地获得所需的所有Spring依赖和其他相关的依赖,而不必根据示例代码复制粘贴所需的依赖。例如,如果想使用Spring和JPA访问数据库,那么就只需要在项目中添加spring-boot-starter-data-jpa依赖性。

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 technology 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, and you are good to go.

spring-boot-actuator

actuator 允许您监视应用程序并与之交互。Spring Boot Actuator 会提供 actuator 所需的基础功能。包括使用注解来配置 Actuator Endpoint。该模块提供了许多 endpoints,包括HealthEndpointEnvironmentEndpointBeansEndpoint等。

Actuator endpoints let you monitor and interact with your application. Spring Boot Actuator provides the infrastructure required for actuator endpoints. It contains annotation support for actuator endpoints. This module provides many endpoints, including the HealthEndpointEnvironmentEndpointBeansEndpoint, and many more.

spring-boot-actuator-autoconfigure

基于classpath(jar依赖)和属性集为 actuator endpoints 提供自动配置。例如,如果 Micrometer 在 classpath 内,Spring Boot 会自动配置 MetricsEndpoint。其包含通过 HTTP方式 或 JMX方式 来发布endpoints信息的配置。和Spring Boot AutoConfigure一样,如何用户配置了自定义的bean,自动配置将取消。

This provides auto-configuration for actuator endpoints based on the content of the classpath and a set of properties. For instance, if Micrometer is on the classpath, it will auto-configure the MetricsEndpoint. It contains configuration to expose endpoints over HTTP or JMX. Just like Spring Boot AutoConfigure, this will back away as the user starts to define their own beans.

spring-boot-test

当需要测试项目时,此模块包含的核心项与注解可以为你提供帮助。

This module contains core items and annotations that can be helpful when testing your application.

spring-boot-test-autoconfigure

与其他的Spring Boot自动配置模块类似,该模块也会基于classpath(依赖)来为tests提供自动配置。其包含许多注解,可以自动配置你需要测试的程序。

Like other Spring Boot auto-configuration modules, spring-boot-test-autoconfigure provides auto-configuration for tests based on the classpath. It includes many annotations that can automatically configure a slice of your application that needs to be tested.

spring-boot-loader

Spring Boot Loader提供了一个方式,它允许你构建一个单独的jar文件,使用java -jar启动项目。一般来说,你可以不直接使用spring-boot-loader,而是使用Gradle或Maven插件来代替。

Spring Boot Loader provides the secret sauce that allows you to build a single jar file that can be launched using java -jar. Generally, you will not need to use spring-boot-loader directly but work with the Gradle or Maven plugin instead.

spring-boot-devtools

此模块可以提供一些额外的开发时的特性,例如自动重启,以获得更流畅的开发体验。当运行的是打包后的程序时,此工具将会自动禁用 (换句话说,此工作只在开发环境运行)

The spring-boot-devtools module provides additional development-time features, such as automatic restarts, for a smoother application development experience. Developer tools are automatically disabled when running a fully packaged application.

未来要阅读的功能

在 Spring Boot 的 readme 文件中,我们发现多次提到了自动配置,可见此功能的重要性; 其次还有内嵌容器外部化配置支持,SpringBootApplication提供的便捷启动方法(也即启动流程),还有起步依赖actuatorspring-boot-testsspring-boot-loaderspring-boot-devtools等。

以下为设置的顺序。

  1. 启动流程
  2. 自动配置
  3. 内嵌容器 juejin.cn/post/744193…
  4. 配置支持
  5. 起步依赖
  6. actuator
  7. spring-boot-tests
  8. spring-boot-loader
  9. spring-boot-devtools