侧边栏壁纸
  • 累计撰写 32 篇文章
  • 累计创建 20 个标签
  • 累计收到 17 条评论

通过一个完整的实例学习自定义springboot-starter

heshaohua
2022-03-29 / 0 评论 / 0 点赞 / 621 阅读 / 3,081 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-03-29,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前言:

在我们日常的开发过程中,使用第三方的组件比如Redis、OSS、Shiro、Swagger等等,有的只需要在配置文件里添加一些配置就能使用该组件了,是因为这些组件已经封装了starter了,而有的需要我们通过代码编写一些配置Bean才能使用,这样接入比较复杂。
特别是我们需要封装一些组件给其他服务调用,如果我们提供starter对方接入就变得简单了,只需要在配置文件里加上配置就能自动装配好服务配置了。


模块说明:

  • server 需要自动装配的模块,相当于我们引用的第三方服务包
  • server-starter 自定义的starter模块,这个模块依赖server模块,实际使用时就是引入这个模块jar包
  • web 使用starter的模块,这个模块引入server-starter模块,相当于我们自己的项目

关键代码:
server的TestConfig需要自动装配,同时还能装配TestConfigServe

TestConfig、TestConfigServe
  • 自定义starter:
  1. server-starter pom.xml
<dependencies>
    <!-- 需要自动装配的模块 -->
    <dependency>
        <groupId>com.fay</groupId>
        <artifactId>server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 装配工具包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.4.2</version>
        <optional>true</optional>
    </dependency>
    <!-- 自动装配 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.4.2</version>
    </dependency>
</dependencies>
  1. server-starter 配置类 ConfigProperties
@Data
@ConfigurationProperties("learn")
public class ConfigProperties {
    private boolean enable;
    private String id;
    private String name;

    public ConfigProperties() {
        this.enable = false;
    }
}
  1. server-starter 装配类 TestConfigConfiguration
@Configuration
@ConditionalOnClass(TestConfig.class)
@EnableConfigurationProperties(ConfigProperties.class)
@ConditionalOnProperty(prefix = "learn", name = "enable", havingValue = "true", matchIfMissing = true)
public class TestConfigConfiguration {
    @Resource
    private ConfigProperties configProperties;

    /** 装配 TestConfig */
    @Bean
    @ConditionalOnMissingBean(TestConfig.class)
    public TestConfig config() {
        TestConfig testConfig = new TestConfig();
        testConfig.setId(configProperties.getId());
        testConfig.setName(configProperties.getName());
        return testConfig;
    }

    /**装配 TestConfigServe */
    @Bean
    @ConditionalOnMissingBean(TestConfigServe.class)
    public TestConfigServe serve() {
        return new TestConfigServe(this.config());
    }
}
  1. server-starter resources/META-INF下的factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.fay.learn.config.TestConfigConfiguration
  1. resources/META-INF下的配置提示(非必要,配置了之后web使用时可以提示配置)
{
  "properties": [
    {
      "name": "test.id",
      "type": "java.lang.String",
      "description": "配置ID."
    },{
      "name": "test.name",
      "type": "java.lang.String",
      "description": "配置名称."
    },{
      "name": "test.enable",
      "type": "java.lang.Boolean",
      "description": "配置开关.",
      "defaultValue": false
    }
  ]
}
  • web模块使用server模块:
  1. web pom.xml引入server-starter
<dependencies>
    <dependency>
        <groupId>com.fay</groupId>
        <artifactId>server-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
  1. web application.yml,配置文件添加配置即可
learn:
  id: learn
  name: 齐天大圣
  enable: true
  1. 启动web的服务,访问TestController下的接口验证装配是否成功。
0

评论区