一文读懂Spring Boot各模块组件依赖关系

时间:2022-07-23
本文章向大家介绍一文读懂Spring Boot各模块组件依赖关系,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

spring boot 作为一款开箱即用的框架,在市场上有很高的流行度。但内部依赖错踪复杂,每个模块都有自己专属职责,同时又可以做为其他模块的补充,具有很强的扩展性。

各模块组件依赖图

核心模块

•spring-boot-dependencies

内部声明维护了68个 spring boot官方jar版本号,以及500多个三方jar包版本号

如果你不想采用spring-boot-starter-parent,但依然想使用其便利性,可以采用下面配置:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
</dependencyManagement>

•spring-boot-starter-parent

一般新建一个web工程,其pom.xml文件的<parent>会依赖这个包

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>

缺点:采用单继承方式,所以限制了其固化Maven依赖(仅限于Spring Boot相关),如果想拥有自定义parent 或者依赖Spring Cloud生态。建议采用<dependencyManagement>方式

•spring-boot-autoconfigure

为市场主流的开源框架初始化客户端的Bean实例,所以里面会对开源框架的二方包有依赖。

但又考虑到业务使用方可能只使用其中某几个开源系统,所以jar包依赖采用Optional定义,表明该依赖只能在本项目中传递,不会传递到引用该项目的父项目中,父项目需要主动引用该依赖才行。

内部src/main/java代码则通过@ConditionalOnClass@ConditionalOnWebApplication扫描classpath是否存在对应的类,根据条件结果决定是否对当前的类配置加载并实例化。

start 组件列表

•spring boot官方包装的start组件,大约54个

地址列表:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters

下面是来自民间组织封装的组件:

•mybatis

组件名:mybatis-spring-boot-starter

地址:https://github.com/mybatis/spring-boot-starter

过程:

mybatis-spring-boot-autoconfigure 包:

<dependencies>
    <!-- Compile dependencies -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <!-- Optional dependencies -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mybatis.scripting</groupId>
      <artifactId>mybatis-freemarker</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mybatis.scripting</groupId>
      <artifactId>mybatis-velocity</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mybatis.scripting</groupId>
      <artifactId>mybatis-thymeleaf</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure-processor</artifactId>
      <optional>true</optional>
    </dependency>

    <!-- @ConfigurationProperties annotation processing (metadata for IDEs) -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>    
</dependencies>

说明:spring.factories 里面配置了启动类,借助 spring 框架提供的注解,来初始化一系列 mybatis 客户端的Bean实例,然后交由spring容器统一管理。

mybatis-spring-boot-starter 包:

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
    </dependency>
</dependencies>

说明:只是一层壳,将mybatis所依赖的组件全部汇总到一块,并遵循spring的规范,maven将包引入到工程后,即可开箱即用。

其它组件的封装原理类似。

•druid

组件名:druid-spring-boot-starter

地址:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

•dubbo

组件名:dubbo-spring-boot-starter

地址:https://github.com/apache/dubbo-spring-boot-project

•redisson

组件名:redisson-spring-boot-starter

地址:https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter

•rocketmq

组件名:rocketmq-spring-boot-starter

地址:https://github.com/ThierrySquirrel/rocketmq-spring-boot-starter

•grpc

组件名:grpc-spring-boot-starter

地址:https://github.com/yidongnan/grpc-spring-boot-starter