手写starter springboot

手写starter springboot,第1张

1.手写一个redis-spring-boot-starter,导入依赖

 
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.redisson
            redisson
            3.11.1
        
    

2.定义属性类,记得提供set get方法

@ConfigurationProperties(prefix = "yujie.redission")
public class RedissonProperties {
    private String host="localhost";
    private String password;
    private int port=6379;
    private int timeout;
    private boolean ssl;
}

3.定义自动装配类

@Configuration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {

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

4.在resources文件夹下创建META-INF/spring.factories文件,记得包要改自己的

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yujie.enable_demo.redis.RedissonAutoConfiguration

5.使用maven的install插件进行打包,完成打包即可给其它项目使用此依赖

      
            com.yujie
            redis666-spring-boot-starter
            0.0.1-SNAPSHOT
        

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/801348.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-07
下一篇 2022-05-07

发表评论

登录后才能评论

评论列表(0条)

保存