自定义redis-spring-boot-starter

写本文的原因

1)某大佬问我有没有自定义过starter?没有

2)因为不会,所以学习

3)  没有整合其它技术的小案例不完整,所以选择了个人认为简单的redis,自定义myredis-spring-boot -starter

自定义starter的命名规范

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.2.2</version>
        </dependency>

官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.2</version>
        </dependency>

项目结构及介绍

项目结构

项目介绍

1)此项目为定义一个redis整合SpringBoot的starter,为了方便区别用my前缀标记自定义的类。

2)myredis-spring-boot-autoconfigure:自定义myredis-starter的核心,核心都在这个module中

3)myredis-spring-boot-starter: 仅仅添加了myredis-spring-boot-autoconfigure的依赖 ,目的是隐藏 细节

4)springboot-demo : 引入自定义的starter依赖,进行测试

项目下载

url:

share/StarterDemo at master · cbeann/share · GitHub

注意:

先install 父工程,

在install 子工程 myredis-spring-boot-autoconfigure

在install 子工程 myredis-spring-boot-starter

在添加依赖在运行测试

项目构建

myredis-spring-boot-autoconfigure模块(Maven项目)

添加依赖

        <!-- 引入spring-boot-starter,所有starter的基本配合 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>


        <!--自定义的yml提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.2.RELEASE</version>
            <optional>true</optional>
        </dependency>


        <!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.0.1</version>
        </dependency>

连接redis的参数配置类

package com.myredis;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author CBeann
 * @create 2020-05-30 14:51
 */
@ConfigurationProperties(prefix = "myredis")
public class MyRedisProperties {


    //ip
    private String host;
    //密码
    private String password;


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

自定义myredisTemplate 

package com.myredis;

import redis.clients.jedis.Jedis;

/**
 * @author CBeann
 * @create 2020-05-30 15:03
 */
public class MyRedisTemplate {

    private Jedis jedis;

    public MyRedisTemplate(Jedis jedis) {
        this.jedis = jedis;
    }


    public String  setString(String key ,String val){

        String set = jedis.set(key, val);
        return set;

    }

    public Long  delKey(String key){
        Long del = jedis.del(key);
        return del;

    }

    public MyRedisTemplate() {
    }
}

MyRedisAutoConfigulation自动配置类

package com.myredis;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

/**
 * @author CBeann
 * @create 2020-05-30 14:59
 */
@Configuration
@EnableConfigurationProperties(MyRedisProperties.class)
@ConditionalOnProperty(prefix = "myredis",name = "host",
        matchIfMissing =false)//如果application.yml或者properties中没有myredis.host属性,则此类MyRedisAutoConfigulation不注入IOC容器
public class MyRedisAutoConfigulation {

    @Bean
    public Jedis jedis(MyRedisProperties myRedisProperties) {

        //获取redis的参数
        String host = myRedisProperties.getHost();
        String password = myRedisProperties.getPassword();
        // 连接redis服务
        Jedis jedis = new Jedis(host, 6379);
        jedis.auth(password);

        return jedis;


    }

    @Bean
    public MyRedisTemplate myRedisTemplate(Jedis jedis) {
        return new MyRedisTemplate(jedis);
    }


}

创建spring.factories

在resources下创建目录META-INF,在META-INF下创建spring.factories,即resources/META-INF/spring.factories

其中等号(=)左边为固定值,右边为自定义的自动配置类

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.myredis.MyRedisAutoConfigulation

 安装到本地仓库

clean->install

myredis-spring-boot-starter(Maven项目)

添加上面模块的依赖

 <dependency>
           <groupId>com.cbeann</groupId>
           <artifactId>myredis-spring-boot-autoconfigure</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>

安装到本地仓库

clean->install

springboot-demo模块(SpringBoot项目+web)

项目基础

SpringBoot+web

修改pom文件

  <!--自定义starter-->
        <dependency>
            <groupId>com.cbeann</groupId>
            <artifactId>myredis-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

添加application.yml配置信息

myredis.host=39.105.30.146
myredis.password=123456

创建controller进行测试

package com.example.springbootdemo.controller;

import com.myredis.MyRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalTime;

/**
 * @author CBeann
 * @create 2020-05-30 15:25
 */
@RestController
public class HelloController {

    @Autowired
    private MyRedisTemplate myRedisTemplate;


    @RequestMapping("/hello")
    public String hello(){
        myRedisTemplate.setString("key2", LocalTime.now().toString());
        return LocalTime.now().toString();
    }

//    @RequestMapping("/hello")
//    public String hello2(){
//        return LocalTime.now().toString();
//    }
}

测试结果

http://localhost:8080/hello

redis中添加了key,value数据

总结

1)yml提示

在myredis-spring-boot-autoconfigure中添加依赖,并且类(MyRedisProperties )上有注解

<!--自定义yml提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.2.RELEASE</version>
            <optional>true</optional>
        </dependency>

参考:自定义starter并在springboot配置文件中生成提示_枸杞泡茶的博客-CSDN博客

2)如果没有上图中的属性,证明我不用redis,系统不应该报错

如果我没有配置myredis的属性,我也没用redis,正常的逻辑是不能报错。

报错的原因是你没有配置myredis的信息,但是他还是加载Jedis并且创建连接,那肯定是报错了,所以不让他加载MyRedisAutoConfigulation这个类,那么这个关于自定义Starter的类就全不让它加载,那么就和没有引入这个依赖一样

如下面代码所示, 如果application.yml或者properties中没有myredis.host属性,则此类MyRedisAutoConfigulation不注入IOC容器

@ConditionalOnProperty(prefix = "myredis",name = "host",
        matchIfMissing =false)//如果application.yml或者properties中没有myredis.host属性,则此类MyRedisAutoConfigulation不注入IOC容器
public class MyRedisAutoConfigulation {

参考

关与 @EnableConfigurationProperties 注解

关与 @EnableConfigurationProperties 注解 - 简书

自定义starter并在springboot配置文件中生成提示

自定义starter并在springboot配置文件中生成提示_枸杞泡茶的博客-CSDN博客

SpringBoot 自定义starter

第五篇 : SpringBoot 自定义starter - 简书

shiro-redis-spring-boot-starter是一个用于集成Apache Shiro和RedisSpring Boot Starter项目。Apache Shiro是一个强大而灵活的Java安全框架,用于身份验证、授权和会话管理等安全功能。而Redis是一个高性能的内存数据库,其具有快速的数据存取能力和持久化支持。 shiro-redis-spring-boot-starter提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易。通过使用该Starter,我们可以方便地将Shiro的会话管理功能存储到Redis中,从而支持分布式环境下的会话共享和管理。 使用shiro-redis-spring-boot-starter可以带来以下好处: 1. 分布式环境的会话共享:通过将Shiro的会话数据存储到Redis中,不同的应用节点可以共享同一个会话,从而实现分布式环境下的会话管理和跨节点的身份验证和授权。 2. 高可用性和性能:Redis作为一个高性能的内存数据库,具有出色的数据读写能力和持久化支持,可以提供可靠的会话存储和高性能的数据访问能力。 3. 简化配置和集成:shiro-redis-spring-boot-starter提供了封装好的配置和集成方式,减少了我们自己实现集成的复杂性和工作量。 总结来说,shiro-redis-spring-boot-starter为我们提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易和高效。通过它,我们可以实现分布式环境下的会话共享和管理,提供高可用性和性能的数据存取能力,同时简化了配置和集成的复杂性。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CBeann

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值