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

706 阅读2分钟

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

前言

  在我们日常的开发过程中,使用第三方的组件比如Redis、OSS、Shiro、Swagger等等,有的只需要在配置文件里添加一些配置就能使用该组件了,是因为这些组件已经封装了starter了,而有的需要我们通过代码编写一些配置Bean才能使用,这样对于每一个项目接入使用都要写一遍配置,比较繁琐而且容易出错。
另外在封装一些组件给其他项目引用时,如果提供starter对方接入就变得简单了,只需要在配置文件里加上配置就能自动装配好服务配置了。

示例代码:gitee.com/GitFay/star…


模块说明

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

实现效果

  假设serverTestConfigTestConfigServe需要自动装配,在其他项目需要使用server服务时,只需要引入封装好的server-starter依赖,写好配置文件,然后如下注入就可以使用了。

@Resource
private TestConfig testConfig;
@Resource
private TestConfigServe 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
/**
 * @ConditionalOn...相关注解的作用:在满足指定条件的时候才将某个 bean 装载到应用上下文中。
 *    @ConditionalOnClass:存在指定类时才装载
 *    @ConditionalOnProperty:prefix配置前缀, name属性名, havingValue属性值为该值时装载, matchIfMissing缺失属性时是否装载)
 * @EnableConfigurationProperties注解的作用:启用自定义配置。
 */
@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下的spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.fay.learn.config.TestConfigConfiguration
  1. resources/META-INF下的配置提示additional-spring-configuration-metadata.json(非必要,配置了之后web使用时可以提示配置)
{
    "properties":[
        {
            "name":"learn.id",
            "type":"java.lang.String",
            "description":"ID"
        },
        {
            "name":"learn.name",
            "type":"java.lang.String",
            "description":"名称"
        },
        {
            "name":"learn.enable",
            "type":"java.lang.Boolean",
            "description":"开关",
            "defaultValue":false
        }
    ],
    // 配置的可选值,非必要
    "hints":[
        {
            "name":"learn.enable",
            "values":[
                {
                    "value":true,
                    "description":"enable元数据true"
                },
                {
                    "value":false,
                    "description":"enable元数据false"
                }
            ]
        }
    ],
    // 配置分组
    "groups":[
        {
            "sourceType":"com.fay.learn.ConfigProperties",
            "type":"com.fay.learn.ConfigProperties",
            "name":"learn",
            "description":"group描述."
        }
    ]
}

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下的接口验证装配是否成功。