自定义Spring Boot Starter

简介: 在使用Spring Boot开发的时候,我们会用到各种各样的Spring-boot-Starter,不过那些Starter都是常用的,在不同的公司,因不同的业务场景,内部的库也不同,有时需要做一个自己的starter,方便后面加快开发速度。

在使用Spring Boot开发的时候,我们会用到各种各样的Spring-boot-Starter,不过那些Starter都是常用的,在不同的公司,因不同的业务场景,内部的库也不同,有时需要做一个自己的starter,方便后面加快开发速度。

知识点

  • @ConfigurationProperties starter的配置属性
  • @EnableConfigurationProperties 启用配置属性
  • spring.factories 可以指定自动配置类
  • spring-configuration-metadata.json 配置属性的补全功能

过程

做一下演示,内部的代码不方便公开。

  1. 新建XXXProperties类,这个类名可以自定义,以Properties为结尾是为了方便标识这个类是Properties功能。

比如RedisProperties


img_8fff2e3021f0e0d3681e2fd207f13251.png
image.png

我们新建TestProperties,这里我省略了getter和setter,自己写的时候加上

@Data
@ConfigurationProperties(prefix = "test")
public class TestProperties {
    private String name;
    private String desc;

    private Nest nest;
    public static class Nest {
        private String nestName;
    }
}
  1. 新建真正的配置类,类名为TestAutoConfigurer,这个根据自己的选择,建合适的类名

参考Redis的


img_bc874ea4b3e6b1001c4b3671975f3e7c.png
image.png

我们自己的

@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfigurer {

    private TestProperties testProperties;

    public TestAutoConfigurer(TestProperties testProperties) {
        this.testProperties = testProperties;
    }

    ...
}

  1. 建立好上面两个类之后,根据业务建立,以及配置的属性就可以建立Bean了。一般会用到@conditonalOnMissingBean(xxBean.class)


    img_054a14f25f66bfac9269e4866cbf6ad0.png
    image.png
  1. 配置,运行并且测试
    配置文件里面
test:
  name: aihe
  nest:
    nestName: nestAihe

img_4c324ca2e11f08bd7443b92fb79271d9.png
image.png

可以看到已经注入进来了。

  1. Spring Boot在启动的时候会默认扫描当前包目录下的所有内容,当前包下带有Spring相关注解的都会生效。

假如我们的自动配置类,不在当前包下怎么办呢?
在resource目录下新建这两个文件


img_133051596b894f8a367b38b4a1f57e72.png
image.png
  1. spring.factories指定自动配置类,这样Spring Boot一定会启用指定的配置类。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
me.aihe.ShiroCasAutoConfigurer
  1. spring-configuration-metadata.json用来做配置的时候自动补全的功能, 如图所示的自动补全,spring-configuration-metadata.json这个文件作用。


    img_e46f5593edc421f2a82c154b96930548.png
    image.png

这个文件主要分为两部分,一个groups一个properties,其余的因为是json格式,比较容易理解,自己写就好了

img_8c9503eabd632aa43af53504a0b4c7a6.png
image.png
  1. 都写好之后,就可以打包测试,供下次使用了。最后的文件结构如下


    img_a700937d531cb1c026faedcd9baaceaf.png
    image.png

最后

主要介绍了下Spring Boot starter的简单制作,感兴趣的可以自己试试。

参考

目录
相关文章
|
22天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
38 0
|
1月前
|
Java Spring 容器
【Java】Spring如何扫描自定义的注解?
【Java】Spring如何扫描自定义的注解?
35 0
|
3月前
|
Prometheus 监控 Cloud Native
Spring Boot如何自定义监控指标
Spring Boot如何自定义监控指标
25 0
|
3月前
|
Java 调度 Maven
Spring Task 自定义定时任务类
Spring Task 自定义定时任务类
36 0
|
1天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
4 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
3天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
28 0
【Spring系列】Sping VS Sping Boot区别与联系
|
1月前
|
前端开发 Java 数据安全/隐私保护
Spring Boot3自定义异常及全局异常捕获
Spring Boot3自定义异常及全局异常捕获
42 1
|
2月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
|
2月前
|
Java 测试技术 Maven
【SpringBoot】仿 spring-boot-project 自定义 starters
【SpringBoot】仿 spring-boot-project 自定义 starters
|
3月前
|
安全 Java 数据安全/隐私保护
Spring Security 自定义认证逻辑
【1月更文挑战第14天】这篇文章的内容基于对Spring Security 认证流程的理解,介绍如何在SpringSecurity中自定义认证逻辑(如验证码登录)
108 3