1. spring-boot-project 项目分析
- spring-boot-project 为 spring-boot 核心技术包,其中包含了 spring-boot 所有基础源码。
- spring-boot-test 为 springboot 的测试包,包含了系统集成测试、部署测试、冒烟测试。
本文以 spring-boot-project 工程包为模板,定制一个属于自己基础工程依赖包。下面着重介绍一下 spring-boot-project 各个模块。
spring-boot:springboot 的框架基础,其中包含了 springboot 应用启动,初始化,启动 banner,springApplication定义、构建器API、应用程序事件和侦听器、应用程序启动跟踪、管理功能。
spring-boot-dependencies:空文件包,此包是对 spring-boot 进行了依赖管理。在构建大型微服务框架时常常会引入此包,统一对依赖包进行版本管理。
spring-boot-parent:此包的父类为 spring-boot-dependencies,提供了 springBoot 快速开发,和 spring-boot-dependencies 区别在于此包适合较少自定义的应用,当自己公司有自己定义的基础框架时,springboot 官网建议使用 spring-boot-dependencies 去构架,这样有利于 maven 依赖版本的管理。
spring-boot-starters:这个模块有很多 starter 子模块,平时用的也是非常多的,Starters 可以作为一组依赖配置信息放在你项目的依赖配置中。从中您可以获得所需的所有 Spring 及其相关技术的一站式服务而无需搜索项目的配置方法并复制粘贴项目所需的依赖配置信息。如果你想使用 Spring JPA 作为数据库访问中间层,仅仅需要将 spring-boot-starter-data-jpa 加入你的项目依赖中, 即可使用 Spring JPA。
其他模块如有需要大家可以去 Github 获取相关文档或源码。
2. 自定义 starters
2.1 工程结构介绍
- pointer-boot-build - 即父工程,只有一个 pom 文件,主要记录一些项目信息及版本控住,这里利用 revision 占位符来统一控制版本。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.pointer.boot</groupId>
<artifactId>pointer-boot-build</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<url>https://gitee.com/pointer_gy/pointer-boot-build</url>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<revision>1.0.0-SNAPSHOT</revision>
<main.basedir>${basedir}</main.basedir>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
</properties>
<modules>
<module>pointer-boot</module>
<module>pointer-boot-dependencies</module>
<module>pointer-boot-parent</module>
<module>pointer-boot-starters</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${flatten-maven-plugin.version}</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pointer-boot
- 自定义的框架基础,这里封装了统一响应结果、基础异常、基础错误码枚举、校验工具类等共用类,同时也引入了一些共用依赖,原则上每个新建的工程都需要引用此 module。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.pointer.boot</groupId>
<artifactId>pointer-boot-parent</artifactId>
<version>${revision}</version>
<relativePath>../pointer-boot-parent</relativePath>
</parent>
<artifactId>pointer-boot</artifactId>
<packaging>jar</packaging>
<properties>
<main.basedir>${basedir}/..</main.basedir>
</properties>
<dependencies>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- MapStruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
</dependency>
<!-- JSR 303 Validation -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Logback -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
</project>
pointer-boot-dependencies
- 空文件包,此包是对 pointer-boot-build 进行了依赖管理。在构建大型微服务框架时常常会引入此包,统一对依赖包进行版本管理。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.pointer.boot</groupId>
<artifactId>pointer-boot-build</artifactId>
<version>${revision}</version>
</parent>
<artifactId>pointer-boot-dependencies</artifactId>
<packaging>pom</packaging>
<properties>
<main.basedir>${basedir}/..</main.basedir>
<spring-boot.version>2.6.12</spring-boot.version>
<!-- Spring Cloud -->
<spring-cloud.version>2021.0.4</spring-cloud.version>
<spring-security-version>5.6.7</spring-security-version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<cola.components.version>4.3.1</cola.components.version>
<!-- Apache Dubbo -->
<dubbo.version>3.0.10</dubbo.version>
<!-- Apache RocketMQ -->
<rocketmq.version>2.2.2</rocketmq.version>
<!-- Apache ElasticJob -->
<elasticjob.version>3.0.1</elasticjob.version>
<!-- Sentry -->
<sentry.version>6.3.0</sentry.version>
<!-- SkyWalking -->
<skywalking.version>8.8.0</skywalking.version>
<!-- Redisson -->
<redisson.version>3.16.8</redisson.version>
<!-- MyBatis Plus -->
<mybatis-plus.version>3.5.2</mybatis-plus.version>
<mybatis-plus-dynamic-datasource.version>3.5.2</mybatis-plus-dynamic-datasource.version>
<druid-version>1.2.13</druid-version>
<!-- ShardingSphere-JDBC -->
<shardingsphere-jdbc.version>5.1.2</shardingsphere-jdbc.version>
<!-- Apache Commons -->
<commons-collections4.version>4.4</commons-collections4.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<commons-math3.version>3.6.1</commons-math3.version>
<commons-text.version>1.9</commons-text.version>
<commons-io.version>2.11.0</commons-io.version>
<commons-fileupload.version>1.4</commons-fileupload.version>
<!-- Guava -->
<guava.version>31.1-jre</guava.version>
<!-- FastJson -->
<fastjson.version>1.2.83</fastjson.version>
<!-- Hutool -->
<hutool.version>5.8.5</hutool.version>
<!-- MapStruct -->
<mapstruct.version>1.5.2.Final</mapstruct.version>
<!-- Easy Excel -->
<easyexcel.version>3.1.1</easyexcel.version>
<!-- Easy Poi -->
<easypoi.version>4.4.0</easypoi.version>
<!-- Lombok -->
<lombok.version>1.18.24</lombok.version>
<!-- jwt -->
<jwt.version>0.9.1</jwt.version>
<!-- Maven Plugin Versions -->
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
<sonar-maven-plugin.version>3.7.0.1746</sonar-maven-plugin.version>
<jacoco-maven-plugin.version>0.8.5</jacoco-maven-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.pointer.boot</groupId>
<artifactId>pointer-boot</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.pointer.boot</groupId>
<artifactId>pointer-boot-starter-web</artifactId>
<version>${revision}</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-components-bom</artifactId>
<version>${cola.components.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-bom</artifactId>
<version>${spring-security-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- RocketMQ -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>${rocketmq.version}</version>
</dependency>
<!-- ElasticJob -->
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-spring-boot-starter</artifactId>
<version>${elasticjob.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-error-handler-dingtalk</artifactId>
<version>${elasticjob.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-error-handler-wechat</artifactId>
<version>${elasticjob.version}</version>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${mybatis-plus-dynamic-datasource.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-version}</version>
</dependency>
<!-- ShardingSphere-JDBC -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>${shardingsphere-jdbc.version}</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-cluster-mode-repository-zookeeper-curator</artifactId>
<version>${shardingsphere-jdbc.version}</version>
</dependency>
<!-- Redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version>
</dependency>
<!-- SkyWalking -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>${skywalking.version}</version>
</dependency>
<!-- Sentry -->
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-bom</artifactId>
<version>${sentry.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commons-beanutils.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- MapStruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<!-- Easy Excel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
<!-- Easy Poi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>${easypoi.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- Jwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven-gpg-plugin.version}</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
pointer-boot-parent
- 此包的父类为 pointer-boot-dependencies,提供了 pointer-boot 快速开发,和 pointer-boot-dependencies 区别在于此包适合较少自定义的应用,这里是我自己定义的基础框架,使用 spring-boot-dependencies 去构架,这样有利于 maven 依赖版本的管理。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.pointer.boot</groupId>
<artifactId>pointer-boot-dependencies</artifactId>
<version>${revision}</version>
<relativePath>../pointer-boot-dependencies</relativePath>
</parent>
<artifactId>pointer-boot-parent</artifactId>
<packaging>pom</packaging>
<properties>
<main.basedir>${basedir}/..</main.basedir>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>
pointer-boot-starters
- 这个模块有很多 starter 子模块,平时用的也是非常多的,Starters 可以作为一组依赖配置信息放在你项目的依赖配置中。这里我只定义了pointer-boot-starter-web
,其封装了一些 web 相关,如: 全局异常捕获处理、全局格式化配置、全局响应拦截器等等。
<?xml version="1.0" encoding="UTF-8"?>
<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>com.pointer.boot</groupId>
<artifactId>pointer-boot-parent</artifactId>
<version>${revision}</version>
<relativePath>../pointer-boot-parent</relativePath>
</parent>
<artifactId>pointer-boot-starters</artifactId>
<packaging>pom</packaging>
<properties>
<main.basedir>${basedir}/..</main.basedir>
</properties>
<modules>
<module>pointer-boot-starter-web</module>
</modules>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<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>com.pointer.boot</groupId>
<artifactId>pointer-boot-starters</artifactId>
<version>${revision}</version>
</parent>
<artifactId>pointer-boot-starter-web</artifactId>
<packaging>jar</packaging>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<!-- Pointer Boot -->
<dependency>
<groupId>com.pointer.boot</groupId>
<artifactId>pointer-boot</artifactId>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
具体实现就不展开讲了,直接上代码:https://gitee.com/pointer_gy/pointer-boot-build.git