SpringBoot 自定义 starter

spring boot 中可以方便的开发自定义 starter依赖,自定义 starter 命名规则一般为 xxx-spring-boot-stater。创建自定义 starter 的主要目的有两个:一是方便引入和管理 maven 依赖,二是向 spring IOC 中注册自定义 bean 对象,方便在项目中直接使用。
假设目前需要创建一个 custom-spring-boot-stater 依赖。
整体开发过程如下。

1. 项目结构

1.1 工程结构

  • custom-starter (maven 项目,父工程,负责依赖包管理和模块管理)
    • custom-spring-boot-autoconfigure (maven 工程,负责自动配置实现)
    • custom-spring-boot-starter (maven 工程,负责引用 autoconfigure 工程)
    • custom-spring-boot-starter-test (spring boot 工程,负责自定义 starter 测试)

在这里插入图片描述

1.2 基础 maven 依赖

1.2.1 custom-starter 项目
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring.boot.version>2.4.5</spring.boot.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
1.2.2 custom-spring-boot-autoconfigure 项目
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>

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

    <!--此依赖可以实现配置文件的自动提示功能-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
</dependencies>

引入 spring-boot-configuration-processor 依赖后,可以实现 spring-configuration-metadata.json 自动生成。spring boot 会根据 json 文件中的内容进行提示。

如果我们想要自定义一些提示,并且限制用户输入某些属性的值,需要在开发 starter 的项目里面的 META-INF 文件夹下,创建additional-spring-configuration-metadata.json文件。

在这里插入图片描述

1.2.3 custom-spring-boot-starter 项目

一般只需要引用 autoconfigure 项目

<dependencies>
    <dependency>
        <groupId>com.ming</groupId>
        <artifactId>custom-spring-boot-autoconfigure</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

2. 代码实现

2.1 custom-spring-boot-autoconfigure

2.1.1 属性配置信息

使用 @ConfigurationProperties 注解,声明属性参数,属性字段上的注释信息会被自动提示。

@ConfigurationProperties(prefix = CustomProperties.MING_PREFIX)
public class CustomProperties {
    public static final String MING_PREFIX = "ming";

    /**
     * 姓名
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 家庭地址
     */
    private String address;
    
    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
2.1.2 自动配置类

使用 @Configuration 注解,声明该类是一个配置类

使用 @EnableConfigurationProperties ,将外部配置信息读取到配置类中

@Configuration
@EnableConfigurationProperties(CustomProperties.class)
public class CustomAutoConfigure {
    private CustomProperties customProperties;

    // 注意这里直接传进来了配置类对象参数,是由 IOC 容器创建并传入的,不能通过 @Autowired 获取
    public CustomAutoConfigure(CustomProperties customProperties) {
        this.customProperties = customProperties;
    }

    @Bean
    public MyStudent myStudent() {
        MyStudent myStudent = new MyStudent(customProperties.getName(), customProperties.getAge(), customProperties.getAddress());
        return myStudent;
    }

}
2.1.3 bean 实体类

bean 实体类为注入到 IOC 中的对象,使用外部配置信息进行初始化,然后在自动配置类中使用 @Bean 进行注入

public class MyStudent {
    private String name;
    private Integer age;
    private String address;

    public MyStudent() {
    }

    public MyStudent(String name, Integer age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
2.1.4 spring.factories

在 resource 目录下,创建 META-INF 目录,并创建 spring.factories 文件,文件中配置好启用自动配置的类路径

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ming.custom.spring.boot.autoconfigure.CustomAutoConfigure

在这里插入图片描述

2.2 custom-spring-boot-starter

starter 项目一般无需写代码

3. 测试

3.1 引入自定义 starter

<dependency>
    <groupId>com.ming</groupId>
    <artifactId>custom-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

3.2 增加配置信息

在这里插入图片描述

3.3 获取 bean 对象

@RestController
public class MyStudentController {

    @Autowired
    private MyStudent myStudent;


    @GetMapping("/show")
    public String showStudent() {
       return myStudent.getName() + myStudent.getAge() + myStudent.getAddress();
    }
}

测试可以正常获取到 autoconfigure 中注入的 bean 对象。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值