• Spring boot 自定义 Starter 及 自动配置


    Starter 组件简介

    Starter 组件是 Spring boot 的一个核心特性,Starter组件的出现极大的简化了项目开发,例如在项目中使用的pom.xml文件中添加以下配置:

    
        org.springframework.boot
        spring-boot-starter-web
        2.5.3
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Spring boot 就会自动关联web 开发相关的依赖,如 tomcat 及 spring-mvc等,进而能支持web开发,同时相关技术也将实现自动配置,避免了繁琐的配置文件。

    Starter 组件 使开发者不需要关注各种依赖库的处理、不需要具体的配置信息,由Spring boot 自动完成class的发现并加载。

    利用Starter实现自动化配置需要两个条件:Maven依赖以及配置文件。

    Maven依赖实际上就是导入一个JAR,然后Springboot启动时候就会找到这个jar包中 resource/META-INF/spring.factories文件,根据文件的配置找到相关的实现类。以上就是spring boot 的自动装配机制,如果理解该机制的话,很容易就能手写一个 starter 组件。

    对starter组件我们可以简单理解为:

    • 每个不同的starter组件实际上完成自身的功能逻辑,然后对外提供一个bean对象让别人调用
    • 对外提供的bean通过自动装配机制注入到IOC容器中

    自定义 Starter 组件

    要实现一个自己的 starter 组件其实也很简单,首先我们需要明确我们需要做那些事:

    1. 通过配置类提供对外服务的 bean 对象
    2. 按照自动装配原理完成 spring.factories 的编写
    3. starter 自动属性配置

    Starter 组件

    接下来我们就手写一个 starter 组件,模仿 RedisTemplate,流程如下:

    • 创建一个spring boot 项目 redis-starter-demo

    • 创建MyStarterTemplate

      简单模拟 redis 的 get,set操作:

      public class MyStarterTemplate {
      
          Map<String, String> map = new HashMap<>();
      
          public String put(String key, String value) {
              map.put(key, value);
              return "保存成功";
          }
      
          public String get(String key) {
              return map.get(key);
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 创建配置类

      @Configuration
      public class MyAutoConfiguration {
      
          @Bean
          public MyStarterTemplate myStarterTemplate(){
              return new MyStarterTemplate();
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    • 实现自动装配流程

      在 classpath 下 META-INF目录下创建 spring.factories 文件:

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
        com.example.redisstarterdemo.MyAutoConfiguration
      
      • 1
      • 2
    • 打包发布 maven install

    使用 Starter

    • 创建一个新项目用来引入 starter test-demo

    • pom 文件引入依赖

      <dependency>
          <groupId>com.examplegroupId>
          <artifactId>redis-starter-demoartifactId>
          <version>0.0.1-SNAPSHOTversion>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 编写接口 ,直接 Autowired 注入MyStarterTemplate

      @RestController
      @RequestMapping(value = "/myRedis")
      public class RedisStarterController {
      
          @Autowired
          private MyStarterTemplate myStarterTemplate;
      
          @RequestMapping("/save")
          public String save(@RequestParam String key, @RequestParam String value) {
              myStarterTemplate.put(key,value);
              return "ok";
          }
      
          @RequestMapping("/get")
          public String get(@RequestParam String key) {
              return  myStarterTemplate.get(key);
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19

    以上便是一个完成的自定义 Starter,如果搞懂SPI机制的话,现在应该觉得很简单单了吧?

    Starter 传参

    以上是一个最简单的 starter 组件,但是有这么一种情况,我们还以 redis 为例,在 redis 中我们是可以通过在配置文件中来设置地址、账号密码等信息的,那这种情况我们应该如何操作呢?

    • 我们还是在上面的项目中继续添加代码

    • 创建需要注入的 bean 类

      这里我们通过对外暴露一个创建方法用来给对象传递地址和端口号;

      public class MyRedis {
      
          public String say() {
              return "my host:" + host + ",port:" + port;
          }
      
          public static MyRedis create(String host, Integer port) {
              return new MyRedis(host, port);
          }
      
          public MyRedis(String host, Integer port) {
              this.host = host;
              this.port = port;
          }
      
          private String host;
      
          private Integer port;
      
          public String getHost() {
              return host;
          }
      
          public void setHost(String host) {
              this.host = host;
          }
      
          public Integer getPort() {
              return port;
          }
      
          public void setPort(Integer port) {
              this.port = port;
          }
      }
      
      • 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
    • 创建属性类

      @ConfigurationProperties(prefix = "redis")的作用是读取 application中以 redis 开头的数据,还有其他几种读取配置文件的注解可以自行研究。

      由于我们这里设置了默认值,所以如果不配置的话也不影响,会使用默认值传递给 bean 。

      @ConfigurationProperties(prefix = "redis")
      public class MyProperties {
      
          private String host = "127.0.0.1";
      
          private Integer port = 6379;
      
          public String getHost() {
              return host;
          }
      
          public void setHost(String host) {
              this.host = host;
          }
      
          public Integer getPort() {
              return port;
          }
      
          public void setPort(Integer port) {
              this.port = port;
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
    • 创建配置类

      使用配置文件,将相关信息传递给 bean 类。

      @Configuration
      @EnableConfigurationProperties(MyProperties.class)
      public class MyRedisConfiguration {
      
          @Bean
          public MyRedis myRedis(MyProperties properties) {
              return MyRedis.create(properties.getHost(), properties.getPort());
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    • 实现自动装配流程

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
        com.example.redisstarterdemo.demo1.MyAutoConfiguration,\
        com.example.redisstarterdemo.demo2.MyRedisConfiguration
      
      • 1
      • 2
      • 3
    • 在项目中引用还是同样的操作,如果想要设置属性的话,只需要在配置文件中编写相关信息即可:

      redis.host=196.128.255.255
      redis.port=9999
      
      • 1
      • 2

    自身与第三方维护

    针对 spring boot 的starter 组件分为两类:

    1. springboot 自身维护的starter组件
      • 所有的 starter 组件自身不带 spring.factories 文件,都集中在spring-boot-autoconfigure 包下的 EnableAutoConfiguration
      • springboot 装配这些配置类是由条件的,不会将所有的都一股脑都加载进来,假设我没用到 redis 的话就不会引入相关依赖包,这样根据@ConditionalOnClass(RedisOperations.class)在classpath下找不到RedisOperations类,就不会加载该配置类。
      • 自身维护的starter组件的命名为:spring-boot-starter-xxx
    2. 第三方维护的starter 组件
      • 在自己的 jar 中维护 spring.factories 文件
      • 命名方式:xxx-spring-boot-starter
  • 相关阅读:
    小程序如何监听页面的滚动事件
    2 errors and 0 warnings potentially fixable with the `--fix` option.(Vue后台管理系统)
    HTB靶场之OnlyForYou
    将 N 叉树编码为二叉树
    Google Play App送审被拒-违反权限政策
    【uniapp】JavaScript基础学习-20231027
    Vue 3 属性绑定细节
    【Kaggle】如何快速上手少走弯路?
    【Pytorch】一文搞懂nn.Conv2d的groups参数的作用
    【Spring】使用xml配置AOP
  • 原文地址:https://blog.csdn.net/qq_41432730/article/details/128212766