Spring boot 自定义 Starter 及 自动配置
创始人
2024-03-24 00:34:28

Starter 组件简介

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

org.springframework.bootspring-boot-starter-web2.5.3

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 map = new HashMap<>();public String put(String key, String value) {map.put(key, value);return "保存成功";}public String get(String key) {return map.get(key);}}
    
  • 创建配置类

    @Configuration
    public class MyAutoConfiguration {@Beanpublic MyStarterTemplate myStarterTemplate(){return new MyStarterTemplate();}}
    
  • 实现自动装配流程

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

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

使用 Starter

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

  • pom 文件引入依赖

    com.exampleredis-starter-demo0.0.1-SNAPSHOT
    
    
  • 编写接口 ,直接 Autowired 注入MyStarterTemplate

    @RestController
    @RequestMapping(value = "/myRedis")
    public class RedisStarterController {@Autowiredprivate 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);}}
    

以上便是一个完成的自定义 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;}
    }
    
  • 创建属性类

    @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;}}
    
  • 创建配置类

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

    @Configuration
    @EnableConfigurationProperties(MyProperties.class)
    public class MyRedisConfiguration {@Beanpublic MyRedis myRedis(MyProperties properties) {return MyRedis.create(properties.getHost(), properties.getPort());}}
    
  • 实现自动装配流程

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

    redis.host=196.128.255.255
    redis.port=9999
    

自身与第三方维护

针对 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

相关内容

热门资讯

“小爱同学”不是想喊就能喊 (来源:法治日报)转自:法治日报□ 本报记者   罗莎莎  □ 本报通讯员 刘晓慧 张海陵  科技飞...
以文塑城 以旅兴城 (来源:衢州日报)转自:衢州日报  记者 赵凯怡  当人文温度融入城市治理的肌理,衢州将千年文脉转化...
中煤(宣威)新能源有限公司成立... 经济观察网 天眼查App显示,12月3日,中煤(宣威)新能源有限公司成立,法定代表人为杨航行,注册资...
深化普法长效机制,打造法治宣传... (来源:法治日报)转自:法治日报□ 周元卿  法治宣传教育是全面依法治国的长期基础性工作。党的二十届...
《雨中曲》等经典剧目岁末年初蓉... 转自:成都日报锦观《雨中曲》等经典剧目岁末年初蓉城上演 本报讯 (成都日报锦观新闻记者 余力)...