【SpringBoot】仿 spring-boot-project 自定义 starters

1. spring-boot-project 项目分析

【SpringBoot】仿 spring-boot-project 自定义 starters_第1张图片

  • 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 工程结构介绍

【SpringBoot】仿 spring-boot-project 自定义 starters_第2张图片

  1. pointer-boot-build - 即父工程,只有一个 pom 文件,主要记录一些项目信息及版本控住,这里利用 revision 占位符来统一控制版本。

<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.0modelVersion>

    <groupId>com.pointer.bootgroupId>
    <artifactId>pointer-boot-buildartifactId>
    <version>${revision}version>
    <packaging>pompackaging>
    <url>https://gitee.com/pointer_gy/pointer-boot-buildurl>

    <licenses>
        <license>
            <name>Apache License, Version 2.0name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txturl>
            <distribution>repodistribution>
        license>
    licenses>

    <properties>
        <revision>1.0.0-SNAPSHOTrevision>
        <main.basedir>${basedir}main.basedir>

        <flatten-maven-plugin.version>1.2.7flatten-maven-plugin.version>
    properties>

    <modules>
        <module>pointer-bootmodule>
        <module>pointer-boot-dependenciesmodule>
        <module>pointer-boot-parentmodule>
        <module>pointer-boot-startersmodule>
    modules>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojogroupId>
                    <artifactId>flatten-maven-pluginartifactId>
                    <version>${flatten-maven-plugin.version}version>
                    <configuration>
                        <updatePomFile>trueupdatePomFile>
                        <flattenMode>resolveCiFriendliesOnlyflattenMode>
                    configuration>
                    <executions>
                        <execution>
                            <id>flattenid>
                            <phase>process-resourcesphase>
                            <goals>
                                <goal>flattengoal>
                            goals>
                        execution>
                        <execution>
                            <id>flatten-cleanid>
                            <phase>cleanphase>
                            <goals>
                                <goal>cleangoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>flatten-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>
  1. pointer-boot - 自定义的框架基础,这里封装了统一响应结果、基础异常、基础错误码枚举、校验工具类等共用类,同时也引入了一些共用依赖,原则上每个新建的工程都需要引用此 module。

<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.0modelVersion>
    <parent>
        <groupId>com.pointer.bootgroupId>
        <artifactId>pointer-boot-parentartifactId>
        <version>${revision}version>
        <relativePath>../pointer-boot-parentrelativePath>
    parent>

    <artifactId>pointer-bootartifactId>
    <packaging>jarpackaging>

    <properties>
        <main.basedir>${basedir}/..main.basedir>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-collections4artifactId>
        dependency>

        
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
        dependency>

        
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
        dependency>

        
        <dependency>
            <groupId>org.mapstructgroupId>
            <artifactId>mapstructartifactId>
        dependency>
        <dependency>
            <groupId>org.mapstructgroupId>
            <artifactId>mapstruct-processorartifactId>
        dependency>

        
        <dependency>
            <groupId>org.hibernate.validatorgroupId>
            <artifactId>hibernate-validatorartifactId>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
        dependency>
    dependencies>
project>
  1. pointer-boot-dependencies - 空文件包,此包是对 pointer-boot-build 进行了依赖管理。在构建大型微服务框架时常常会引入此包,统一对依赖包进行版本管理。

<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.0modelVersion>
    <parent>
        <groupId>com.pointer.bootgroupId>
        <artifactId>pointer-boot-buildartifactId>
        <version>${revision}version>
    parent>

    <artifactId>pointer-boot-dependenciesartifactId>
    <packaging>pompackaging>

    <properties>
        <main.basedir>${basedir}/..main.basedir>

        <spring-boot.version>2.6.12spring-boot.version>
        
        <spring-cloud.version>2021.0.4spring-cloud.version>
        <spring-security-version>5.6.7spring-security-version>
        <spring-cloud-alibaba.version>2021.0.4.0spring-cloud-alibaba.version>
        <cola.components.version>4.3.1cola.components.version>
        
        <dubbo.version>3.0.10dubbo.version>
        
        <rocketmq.version>2.2.2rocketmq.version>
        
        <elasticjob.version>3.0.1elasticjob.version>
        
        <sentry.version>6.3.0sentry.version>
        
        <skywalking.version>8.8.0skywalking.version>
        
        <redisson.version>3.16.8redisson.version>
        
        <mybatis-plus.version>3.5.2mybatis-plus.version>
        <mybatis-plus-dynamic-datasource.version>3.5.2mybatis-plus-dynamic-datasource.version>
        <druid-version>1.2.13druid-version>
        
        <shardingsphere-jdbc.version>5.1.2shardingsphere-jdbc.version>

        
        <commons-collections4.version>4.4commons-collections4.version>
        <commons-beanutils.version>1.9.4commons-beanutils.version>
        <commons-math3.version>3.6.1commons-math3.version>
        <commons-text.version>1.9commons-text.version>
        <commons-io.version>2.11.0commons-io.version>
        <commons-fileupload.version>1.4commons-fileupload.version>
        
        <guava.version>31.1-jreguava.version>
        
        <fastjson.version>1.2.83fastjson.version>
        
        <hutool.version>5.8.5hutool.version>
        
        <mapstruct.version>1.5.2.Finalmapstruct.version>
        
        <easyexcel.version>3.1.1easyexcel.version>
        
        <easypoi.version>4.4.0easypoi.version>
        
        <lombok.version>1.18.24lombok.version>
        
        <jwt.version>0.9.1jwt.version>

        
        <maven-gpg-plugin.version>3.0.1maven-gpg-plugin.version>
        <sonar-maven-plugin.version>3.7.0.1746sonar-maven-plugin.version>
        <jacoco-maven-plugin.version>0.8.5jacoco-maven-plugin.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.pointer.bootgroupId>
                <artifactId>pointer-bootartifactId>
                <version>${revision}version>
            dependency>
            <dependency>
                <groupId>com.pointer.bootgroupId>
                <artifactId>pointer-boot-starter-webartifactId>
                <version>${revision}version>
            dependency>

            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring-boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.colagroupId>
                <artifactId>cola-components-bomartifactId>
                <version>${cola.components.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>org.springframework.securitygroupId>
                <artifactId>spring-security-bomartifactId>
                <version>${spring-security-version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-dependencies-bomartifactId>
                <version>${dubbo.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-dependencies-zookeeperartifactId>
                <version>${dubbo.version}version>
                <type>pomtype>
                <exclusions>
                    <exclusion>
                        <artifactId>log4jartifactId>
                        <groupId>log4jgroupId>
                    exclusion>
                    <exclusion>
                        <artifactId>slf4j-log4j12artifactId>
                        <groupId>org.slf4jgroupId>
                    exclusion>
                exclusions>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>${dubbo.version}version>
            dependency>

            
            <dependency>
                <groupId>org.apache.rocketmqgroupId>
                <artifactId>rocketmq-spring-boot-starterartifactId>
                <version>${rocketmq.version}version>
            dependency>

            
            <dependency>
                <groupId>org.apache.shardingsphere.elasticjobgroupId>
                <artifactId>elasticjob-lite-spring-boot-starterartifactId>
                <version>${elasticjob.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.shardingsphere.elasticjobgroupId>
                <artifactId>elasticjob-error-handler-dingtalkartifactId>
                <version>${elasticjob.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.shardingsphere.elasticjobgroupId>
                <artifactId>elasticjob-error-handler-wechatartifactId>
                <version>${elasticjob.version}version>
            dependency>

            
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>${mybatis-plus.version}version>
            dependency>
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>dynamic-datasource-spring-boot-starterartifactId>
                <version>${mybatis-plus-dynamic-datasource.version}version>
            dependency>

            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>${druid-version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.shardingspheregroupId>
                <artifactId>shardingsphere-jdbc-core-spring-boot-starterartifactId>
                <version>${shardingsphere-jdbc.version}version>
                <exclusions>
                    <exclusion>
                        <artifactId>log4jartifactId>
                        <groupId>log4jgroupId>
                    exclusion>
                exclusions>
            dependency>
            <dependency>
                <groupId>org.apache.shardingspheregroupId>
                <artifactId>shardingsphere-cluster-mode-repository-zookeeper-curatorartifactId>
                <version>${shardingsphere-jdbc.version}version>
            dependency>

            
            <dependency>
                <groupId>org.redissongroupId>
                <artifactId>redisson-spring-boot-starterartifactId>
                <version>${redisson.version}version>
            dependency>

            
            <dependency>
                <groupId>org.apache.skywalkinggroupId>
                <artifactId>apm-toolkit-logback-1.xartifactId>
                <version>${skywalking.version}version>
            dependency>

            
            <dependency>
                <groupId>io.sentrygroupId>
                <artifactId>sentry-bomartifactId>
                <version>${sentry.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-collections4artifactId>
                <version>${commons-collections4.version}version>
            dependency>
            <dependency>
                <groupId>commons-beanutilsgroupId>
                <artifactId>commons-beanutilsartifactId>
                <version>${commons-beanutils.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-math3artifactId>
                <version>${commons-math3.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-textartifactId>
                <version>${commons-text.version}version>
            dependency>
            <dependency>
                <groupId>commons-iogroupId>
                <artifactId>commons-ioartifactId>
                <version>${commons-io.version}version>
            dependency>
            <dependency>
                <groupId>commons-fileuploadgroupId>
                <artifactId>commons-fileuploadartifactId>
                <version>${commons-fileupload.version}version>
            dependency>

            
            <dependency>
                <groupId>com.google.guavagroupId>
                <artifactId>guavaartifactId>
                <version>${guava.version}version>
            dependency>

            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>${fastjson.version}version>
            dependency>

            
            <dependency>
                <groupId>cn.hutoolgroupId>
                <artifactId>hutool-allartifactId>
                <version>${hutool.version}version>
            dependency>

            
            <dependency>
                <groupId>org.mapstructgroupId>
                <artifactId>mapstructartifactId>
                <version>${mapstruct.version}version>
            dependency>
            <dependency>
                <groupId>org.mapstructgroupId>
                <artifactId>mapstruct-processorartifactId>
                <version>${mapstruct.version}version>
            dependency>

            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>easyexcelartifactId>
                <version>${easyexcel.version}version>
            dependency>

            
            <dependency>
                <groupId>cn.afterturngroupId>
                <artifactId>easypoi-spring-boot-starterartifactId>
                <version>${easypoi.version}version>
            dependency>

            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>

            
            <dependency>
                <groupId>io.jsonwebtokengroupId>
                <artifactId>jjwtartifactId>
                <version>${jwt.version}version>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>${spring-boot.version}version>
                plugin>

                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-gpg-pluginartifactId>
                    <version>${maven-gpg-plugin.version}version>
                    <executions>
                        <execution>
                            <phase>verifyphase>
                            <goals>
                                <goal>signgoal>
                            goals>
                        execution>
                    executions>
                plugin>
                <plugin>
                    <groupId>org.sonarsource.scanner.mavengroupId>
                    <artifactId>sonar-maven-pluginartifactId>
                    <version>${sonar-maven-plugin.version}version>
                plugin>
                <plugin>
                    <groupId>org.jacocogroupId>
                    <artifactId>jacoco-maven-pluginartifactId>
                    <version>${jacoco-maven-plugin.version}version>
                plugin>
            plugins>
        pluginManagement>
    build>
project>
  1. pointer-boot-parent - 此包的父类为 pointer-boot-dependencies,提供了 pointer-boot 快速开发,和 pointer-boot-dependencies 区别在于此包适合较少自定义的应用,这里是我自己定义的基础框架,使用 spring-boot-dependencies 去构架,这样有利于 maven 依赖版本的管理。

<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.0modelVersion>
    <parent>
        <groupId>com.pointer.bootgroupId>
        <artifactId>pointer-boot-dependenciesartifactId>
        <version>${revision}version>
        <relativePath>../pointer-boot-dependenciesrelativePath>
    parent>

    <artifactId>pointer-boot-parentartifactId>
    <packaging>pompackaging>

    <properties>
        <main.basedir>${basedir}/..main.basedir>

        <java.version>1.8java.version>
        <maven.compiler.source>${java.version}maven.compiler.source>
        <maven.compiler.target>${java.version}maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    properties>

project>
  1. pointer-boot-starters - 这个模块有很多 starter 子模块,平时用的也是非常多的,Starters 可以作为一组依赖配置信息放在你项目的依赖配置中。这里我只定义了 pointer-boot-starter-web,其封装了一些 web 相关,如: 全局异常捕获处理、全局格式化配置、全局响应拦截器等等。

<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.0modelVersion>
    <parent>
        <groupId>com.pointer.bootgroupId>
        <artifactId>pointer-boot-parentartifactId>
        <version>${revision}version>
        <relativePath>../pointer-boot-parentrelativePath>
    parent>

    <artifactId>pointer-boot-startersartifactId>
    <packaging>pompackaging>

    <properties>
        <main.basedir>${basedir}/..main.basedir>
    properties>

    <modules>
        <module>pointer-boot-starter-webmodule>
    modules>

project>

<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.0modelVersion>
    <parent>
        <groupId>com.pointer.bootgroupId>
        <artifactId>pointer-boot-startersartifactId>
        <version>${revision}version>
    parent>

    <artifactId>pointer-boot-starter-webartifactId>
    <packaging>jarpackaging>

    <properties>
        <main.basedir>${basedir}/../..main.basedir>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>com.pointer.bootgroupId>
            <artifactId>pointer-bootartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
    dependencies>
project>

具体实现就不展开讲了,直接上代码:https://gitee.com/pointer_gy/pointer-boot-build.git

你可能感兴趣的:(微服务,Spring,Boot,Java,spring,boot,spring,java)