当前位置:   article > 正文

SpringBoot 高级 原理分析 -- 自定义redis-starter

redis-starter

01: SpringBoot 高级 原理分析 – SpringBoot 自动配置:Condition
02: SpringBoot 高级 原理分析 – 切换内置web服务器
03: SpringBoot 高级 原理分析 – @Enable*注解、@Import注解
04: SpringBoot 高级 原理分析 – @Import注解
05: SpringBoot 高级 原理分析 – @EnableAutoConfiguration 注解
06: SpringBoot 高级 原理分析 – 自定义redis-starter

1. 自定义redis-starter

需求:

自定义redis-starter。要求当导入redis坐标时,SpringBoot自动创建Jedis的Bean。


1.1 参考mybatis-spring-boot-starter

引入mybatis-spring-boot-starter

pom.xml

<!--引入mybatis-stater-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.1.1 参考mybatis-spring-boot-starter起步依赖自带的坐标

在这里插入图片描述

在这里插入图片描述


1.1.2 mybatis-spring-boot-starter起步依赖 核心类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


1.2 分析实现步骤

参照上面的mybatis-spring-boot-starter起步依赖,我们分析出了自定义redis-starter的思路

  • ① 创建 redis-spring-boot-autoconfigure 模块
  • ② 创建 redis-spring-boot-starter 模块,依赖 redis-springboot-autoconfigure的模块
  • ③ 在 redis-spring-boot-autoconfigure 模块中初始化 Jedis 的Bean。并定义META-INF/spring.factories 文件
  • ④ 在测试模块中引入自定义的 redis-starter 依赖,测试获取Jedis 的Bean,操作 redis。

1.2.1 创建2个模块 依赖 redis-springboot-autoconfigure的模块

在这里插入图片描述

依赖 redis-springboot-autoconfigure的模块:

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tian</groupId>
    <artifactId>redis-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redis-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

<!--        引入自定义的redis-spring-boot-autoconfigure-->
        <dependency>
            <groupId>com.tian</groupId>
            <artifactId>redis-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1.2.2 在 redis-spring-boot-autoconfigure 模块中初始化 Jedis 的Bean。并定义META-INF/spring.factories 文件

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tian</groupId>
    <artifactId>redis-spring-boot-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redis-spring-boot-autoconfigure</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

<!--        引入jedis坐标-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
    </dependencies>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在这里插入图片描述

RedisAutoConfiguration.java

package com.tian.config;

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

@Configuration // 声明该类为配置类
@EnableConfigurationProperties(RedisProperties.class)// 让Spring识别RedisProperties里面定义的配置信息
@ConditionalOnClass(Jedis.class)// 只有依赖库中有Jedis.class才会 往IOC中注入redis的bean
public class RedisAutoConfiguration {

    /**
     * 提供Jedis的bean
     */
    @Bean // 注入bean
    @ConditionalOnMissingBean(name = "jedis") // 只有在IOC容量里面没有名为jedis的bean时 才注入名为jedis的bean
    public Jedis jedis(RedisProperties redisProperties) {
        return new Jedis(redisProperties.getHost(), redisProperties.getPort());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

RedisProperties.java

package com.tian.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

// 配置文件中以redis开头的属性都会和本地的redis对应起来
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {

    // 默认为本机
    private String host = "localhost";
    // redis默认端口为6379
    private int port = 6379;


    public String getHost() {
        return host;
    }

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

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

定义META-INF/spring.factories文件:

在这里插入图片描述

说明,这里\表示换行。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.tian.config.RedisAutoConfiguration
  • 1
  • 2

1.2.3 在测试模块中引入自定义的 redis-starter 依赖,测试获取Jedis 的Bean,操作 redis

创建一个测试SringBoot测试模块:

在这里插入图片描述
在这里插入图片描述

编写测试程序:

在这里插入图片描述

package com.tian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import redis.clients.jedis.Jedis;


@SpringBootApplication
public class SpringbootEnableApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
        Jedis jedis = (Jedis) context.getBean("jedis");
        System.out.println("jedis: "+jedis);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

运行结果:

在这里插入图片描述

进行存储数据测试:

  1. 启动redis数据库,查看里面有没有数据

在这里插入图片描述

  1. 测试
    在这里插入图片描述
    在这里插入图片描述

测试配置是否生效:

我们改了参数之后redis就运行不起来了,说明我们的配置生效了
在这里插入图片描述



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/715786
推荐阅读
相关标签
  

闽ICP备14008679号