自定义springboot starter-有demo-可伸手

自定义springboot starter

  1. 准备maven spring 环境
  2. 编写starter逻辑
  3. 抽取配置
  4. 配置spring自动装载

1.准备maven spring 环境

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <protobuf.version>3.12.3</protobuf.version>
    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    <grpc.version>1.29.0</grpc.version>
    <spring-boot.version>2.2.1.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.SR5</spring-cloud.version>
    <java.version>1.8</java.version>
    <protobuf.version>3.12.3</protobuf.version>
    <grpc.version>1.29.0</grpc.version>
</properties>

<dependencies>
    <!--负责从properties文件中读取配置信息-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    <!--实现自动化配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.ofpay/logback-mdc-ttl -->
    <dependency>
        <groupId>com.ofpay</groupId>
        <artifactId>logback-mdc-ttl</artifactId>
        <version>1.0.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/transmittable-thread-local -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>transmittable-thread-local</artifactId>
        <version>2.11.5</version>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.ofpay/logback-mdc-ttl -->
        <dependency>
            <groupId>com.ofpay</groupId>
            <artifactId>logback-mdc-ttl</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/transmittable-thread-local -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>transmittable-thread-local</artifactId>
            <version>2.11.5</version>
        </dependency>


        <!--springcloud依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--springboot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>
    </dependencies>
</dependencyManagement>

2. 编写starter逻辑

git仓库: https://gitee.com/suveng/util-all/tree/b28fedd33e4c8cdf175a1c14e275c70293e1d3b1/alarm-spring-boot-starter

指定我现在提交的版本, 方便看
当然我也提交到了maven 的官方仓库中, 你也可以直接拿来用, 但是希望你经过测试才拿来用, 也可以在上面提issue 或者 通过微信直接联系我

3. 抽取配置

主要抽取starter用到的配置, 依赖项的抽象配置, 放到springboot的配置中,
让用户可以自定义配置

4. 配置spring自动装载

spring加载starter有两种方式
被动, 用户引入starter后需要加载配置才可以使用starter功能, 配置是主动开启的
主动, 用户引入starter后 即可使用starter功能, 配置是被动的, 无感知的, 当然可以通过配置属性来开关, 这个看个人的starter逻辑怎么定义

被动需要配置 resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
my.suveng.alarm.config.AlarmAutoConfig

资料

springboot中starter的编写 - 简书

最详细的自定义Spring Boot Starter开发教程_码农小胖哥-CSDN博客

相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:马嘣嘣 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值