当前位置:   article > 正文

SpringBoot 自定义 starter_spring boot start custom

spring boot start custom

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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

引入 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
2.1.4 spring.factories

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

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

在这里插入图片描述

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>
  • 1
  • 2
  • 3
  • 4
  • 5

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/947754
推荐阅读
相关标签
  

闽ICP备14008679号