1 Starter的命名规范

Starter的命名主要分为两类,一类是官方命名,另一类是自定义组件命名。 官方命名的格式为:spring-boot-starter-模块名称,比如spring-boot-starter-web。

自定义命名格式为:模块名称-spring-boot-starter,比如mybatis-spring-boot-starter。

2 实现基于redission的Starter

创建项目redis-spring-boot-starter

<dependencies>
				<!-- 引入redission -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.11.1</version>
        </dependency>
				<!-- 基础的启动器(starter),是其他 Spring Boot 启动器的基础。-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

定义属性类,实现在application.properties中配置Redis的连接参数,@ConfigurationProperties这个注解的作用是把当前类中的属性和配置文件(properties/yml)中的配置进行绑定,并且前缀是cc.redisson。

@Data
// 在 Spring Boot 2.2 及更高版本中,@ConfigurationProperties 类不再需要 @EnableConfigurationProperties
// 通常与 @Component 一起使用,以便 Spring 能够扫描并注册该类为一个 Bean。
@ConfigurationProperties(prefix = "cc.redission")
public class RedissionProperties {

    private String host;

    private String password;

    private int port;

    private int timeout;

    private boolean ssl;

}

定义需要自动装配的配置类,主要就是把RedissonClient装配到IoC容器,@Conditional0nClass,它表示一个条件,在当前场景中表示的是:在classpath下存在Redisson这个类的时候,RedissonAutoConfiguration才会实现自动装配。

//用于定义配置类。用来定义和配置 Spring 应用程序上下文中的 Bean。
@Configuration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissionProperties.class)
public class RedissionAutoConfiguration {

    @Bean
    RedissonClient redissonClient(RedissionProperties redissonProperties) {
        Config config = new Config();
        String prefix = "redis://";
        if (redissonProperties.isSsl()) {
            prefix = "rediss://";
        }
        SingleServerConfig singleServerConfig = config.useSingleServer().setAddress(prefix + redissonProperties.getHost() + ":" + redissonProperties.getPort())
                .setConnectTimeout(redissonProperties.getTimeout());
        if (!StringUtils.isEmpty(redissonProperties.getPassword())) {
            singleServerConfig.setPassword(redissonProperties.getPassword());
        }
        return Redisson.create(config);
    }
}

resources文件夹下件META-INF文件夹,创建spring.factories 文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\org.example.RedissionAutoConfiguration

安装到maven仓库中

3 使用Starter

创建springboot的web项目,引入依赖

<dependency>
    <groupId>org.example</groupId>
    <artifactId>redis-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
@RestController
public class AController {

    @Autowired
    RedissonClient redissonClient;

    @GetMapping("/a/{value}")
    public String test(@PathVariable String value){
        System.out.println("11111111111111");
        RBucket<String> bucket = redissonClient.getBucket("myKey");

        bucket.set(value);
        return value;
    }
}

使用Starter中定义的bean。