starter项目
先来看一下Starter的官方解释:
Spring Boot Starter 是一种方便的依赖管理方式,它封装了特定功能或技术栈的所有必要依赖项和配置,使得开发者可以快速地将这些功能集成到Spring Boot项目中。Spring Boot官方提供了一系列的Starters,涵盖了从Web开发到安全、数据访问等多个方面
其实Starter是一种SDK思想,基于SDK高度抽象快速构建功能块或技术块是当下企业技术部门实现技术资产快速开发的利器,基于Spring的starter自然就是最高效的方法。
starter项目结构分析
我们先来看一下mybatis-spring-boot-starter的项目结构
抛开mybatis的jdbc等逐步演化的核心功能依赖,只专注我们想要参观的starter部分可以看到,mybatis-spring-boot-starter被引入后是以两个jar的形式存在,即
- mybatis-spring-boot-autoconfigure
- mybatis-spring-boot-starter
其中,mybatis-spring-boot-starter 项目中并无代码,仅仅一个pom文件指明依赖
<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>
<parent>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot</artifactId>
<version>2.1.4</version>
</parent>
<artifactId>mybatis-spring-boot-starter</artifactId>
<name>mybatis-spring-boot-starter</name>
<properties>
<module.name>org.mybatis.spring.boot.starter</module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--mybatis-starter的代码项目-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 支撑mybatis-starter的其他依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
</dependencies>
</project>
也就是说, mybatis-starter与SpringBoot对接的主要逻辑集中在mybatis-spring-boot-autoconfigure 中,mybatis-spring-boot-starter 仅仅是个壳子,它统一管理mybati