Springboot自定义Starter启动器

image-20230626213619598

一、背景介绍

Springboot Starter机制抛弃了过去创建一个Spring项目需要依赖大量繁琐的jar包和配置信息,同时也规避了版本冲突的问题,做Java开发的经历中一定碰到过引入各种依赖出现版本冲突的噩梦,Starter的出现就是为了规避这样的问题,做到同一个依赖的版本统一。本文主要说明Starter的原理和如何自定义一个Starter启动器。

二、自定义Starter

springboot starter利用自动装载的原理,将starter中的配置项自动加载到IoC容器中,降低配置的复杂性。自定义一个starter的主要步骤为:

  1. 引入POM依赖;
  2. 配置和配置文件对应的xxxProperties类;
  3. 配置业务类;
  4. 配置自动配置xxxAutoConfiguration类;
  5. 配置spring.factories文件;
1.配置POM文件
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>

其中spring-boot-autoconfigure是必须要引入的。

2.定义属性配置类xxxProperties
@ConfigurationProperties(prefix = "com.test")
public class MyStarterProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

MyStarterProperties这个文件的属性和配置文件中的配置项是对应的,@ConfigurationProperties(prefix = “com.test”)表示该配置项的key以com.test为前缀。

3.定义自动配置类XXXAutoConfiguration
@EnableConfigurationProperties({
        MyStarterProperties.class,
})

@Configuration
public class MyStarterAutoConfiguration {

    @Bean
    MyStarterTemplate getMyStarterTemplate(MyStarterProperties myStarterProperties) {
        return new MyStarterTemplate(myStarterProperties);
    }
}

MyStarterAutoConfiguration是自动配置类,他会自动加载MyStarterProperties属性配置类,并且会返回MyStarterTemplate这个业务相关的配置类,这个类是和业务相关的,需要我们自己实现。

4.定义业务Bean类
public class MyStarterTemplate {

    MyStarterProperties myStarterProperties;

    public MyStarterTemplate(MyStarterProperties myStarterProperties) {
        this.myStarterProperties = myStarterProperties;
    }

    public void printName() {
        System.out.println("myStarterProperties.getName() = " + myStarterProperties.getName());
    }
}

MyStarterTemplate这个配置类也会被自动加载。

5.定义spring.factories文件

在resources/META-INF创建一个spring.factories文件和spring-configuration-metadata.json文件。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yangnk.threadpoolstarter.myStarter.MyStarterAutoConfiguration
{
  "group": [
    {
      "name": "com.yangnk",
      "type": "com.yangnk.threadpoolstarter.config.MyStarterProperties",
      "sourceType": "com.yangnk.threadpoolstarter.config.MyStarterProperties"
    }
  ],
  "properties": [
    {
      "name": "com.yangnk.name",
      "type": "java.lang.String",
      "description": "my start name",
      "sourceType": "com.yangnk.threadpoolstarter.myStarter.MyStarterProperties",
      "defaultValue": "MyStarterProperties name"
    }
  ]
}

如果不想要手动实现pring-configuration-metadata.json,引入的spring-boot-configuration-processor依赖会帮我们自动生成。

image-20230626204631425

6.打包测试

通过maven的install命令就可以将该依赖打包到本地仓库。在测试的项目中引入该依赖即可,在Properties配置文件中加上配置项。

image-20230626204906443

image-20230626213150625

image-20230626213059333

三、代码实现

myStarterDemo完整代码:https://github.com/yangnk/SpringBoot_Learning/tree/master/MyStarterDemo


参考资料

  1. 手把手教你实现自定义 Spring Boot 的 Starter:https://xie.infoq.cn/article/68621c5f5c1dc16312e0a52e4
  2. SpringBoot自动装配、启动流程及自定义starter:https://juejin.cn/post/7174031833183551519#heading-3
  3. SpringBoot2.x系列教程60–SpringBoot如何自定义Starter启动器?:https://juejin.cn/post/7180097458351898682
  4. 自定义springboot starter,你学废了吗:https://juejin.cn/post/7127468724046528525
  5. SpringBoot SPI 机制和实现自定义 starter:SpringBoot SPI 机制和实现自定义 starter:https://juejin.cn/post/7132132528810852382
  6. :大聪明教你学Java | 深入浅出聊 SpringBoot 中的 starter 机制:https://juejin.cn/post/7242815848087978040
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangnk42

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值