Springboot自定义Starter启动器

简介: Springboot自定义Starter启动器.md

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
目录
相关文章
|
25天前
|
XML Java 数据格式
Springboot中自定义组件
Springboot中自定义组件
|
3月前
|
安全 Java Spring
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)
47 0
|
4月前
|
设计模式 Java 机器人
SpringBoot3自动配置流程 SPI机制 核心注解 自定义starter
SpringBoot3自动配置流程 SPI机制 核心注解 自定义starter
|
8天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
2月前
|
Java 数据库 数据安全/隐私保护
【SpringBoot】Validator组件+自定义约束注解实现手机号码校验和密码格式限制
【SpringBoot】Validator组件+自定义约束注解实现手机号码校验和密码格式限制
118 1
|
2月前
|
前端开发 NoSQL Java
【SpringBoot】秒杀业务:redis+拦截器+自定义注解+验证码简单实现限流
【SpringBoot】秒杀业务:redis+拦截器+自定义注解+验证码简单实现限流
|
2月前
|
存储 NoSQL 前端开发
【SpringBoot】Redis集中管理Session和自定义用户参数解决登录状态及校验问题
【SpringBoot】Redis集中管理Session和自定义用户参数解决登录状态及校验问题
|
2月前
|
Java 容器 Spring
SpringBoot:Bean生命周期自定义初始化和销毁
SpringBoot:Bean生命周期自定义初始化和销毁
|
3月前
|
Java Spring 容器
SpringBoot2 | SpringBoot自定义AutoConfiguration | SpringBoot自定义starter(五)
SpringBoot2 | SpringBoot自定义AutoConfiguration | SpringBoot自定义starter(五)
21 0
|
4月前
|
Java
SpringBoot thymeleaf自定义错误页面
SpringBoot thymeleaf自定义错误页面
23 0