自定义实现spring-boot-starter-data-redis

首先建项目

然后pom里面引入依赖;

 <!-- springboot整合redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

把以前的redis配置文件直接考过来;

@Configuration
public class RedisTestAutoConfigure {

    @Value("${redis.task.host}")
    private String redisHost;

    @Value("${redis.task.port}")
    private int redisPort;

    @Value("${redis.task.pass}")
    private String redisPass;

    @Value("${redis.task.db}")
    private int redisDb;

    @Value("${redis.task.config.timeout}")
    private String timeout;
    @Value("${redis.task.config.maxTotal}")
    private int maxTotal;
    @Value("${redis.task.config.maxIdle}")
    private int maxIdle;
    @Value("${redis.task.config.maxWaitMillis}")
    private int maxWaitMillis;
    @Value("${redis.task.config.minEvictableIdleTimeMillis}")
    private String minEvictableIdleTimeMillis;
    @Value("${redis.task.config.numTestsPerEvictionRun}")
    private int numTestsPerEvictionRun;
    @Value("${redis.task.config.timeBetweenEvictionRunsMillis}")
    private String timeBetweenEvictionRunsMillis;
    @Value("${redis.task.config.testOnBorrow}")
    private Boolean testOnBorrow;
    @Value("${redis.task.config.testWhileIdle}")
    private Boolean testWhileIdle;

    @Bean
    @Primary
    public RedisConnectionFactory taskConnectionFactory() {
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPort(redisPort);
        connectionFactory.setHostName(redisHost);
        connectionFactory.setDatabase(redisDb);
        connectionFactory.setPassword(redisPass);

        //配置连接池属性
        connectionFactory.setTimeout(Integer.parseInt(timeout));
        connectionFactory.getPoolConfig().setMaxIdle(maxIdle);
        connectionFactory.getPoolConfig().setMaxTotal(maxTotal);
        connectionFactory.getPoolConfig().setMaxWaitMillis(maxWaitMillis);
        connectionFactory.getPoolConfig().setMinEvictableIdleTimeMillis(Integer.parseInt(minEvictableIdleTimeMillis));
        connectionFactory.getPoolConfig().setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        connectionFactory.getPoolConfig().setTimeBetweenEvictionRunsMillis(Integer.parseInt(timeBetweenEvictionRunsMillis));
        connectionFactory.getPoolConfig().setTestOnBorrow(testOnBorrow);
        connectionFactory.getPoolConfig().setTestWhileIdle(testWhileIdle);

        return connectionFactory;
    }

    @Bean
    public RedisTemplate taskRedisTemplate() {
        RedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(taskConnectionFactory());
        return template;
    }

}

测试

然后在web里面依赖redis模块,然后在web的spring配置文件中加上redis的参数:
在这里插入图片描述
测试
在这里插入图片描述
访问没问题:
在这里插入图片描述

疑问

感觉好像都很简单,但是当我们把web里面redis配置删除的时候,启动
在这里插入图片描述
报错了,我们在使用spring-boot-starter-data-redis的时候,不加配置,会报错吗?如果是本地安装的redis就不会,因为spring-boot-starter-data-redis给了默认的值,

修改

参考源码进行修改:
在这里插入图片描述
然后看源码的RedisProperties,也是从配置中读取相关连接信息:
在这里插入图片描述
应该加一个redis配置文件类如果配置了redis的参数的话,就加载RedisTemplate,如果没有配置的话,则使用默认的,我们这里,如果没有就直接让报错好了,就不设置默认的了.

新建个RedisProperties类

@ConfigurationProperties("redis.task")
@Data
public class RedisProperties {

    private String host;

    private int port;

    private String pass;

    private int db;

    private Config config;

}

config类,连接池信息类:

@Data
public class Config {

    private String timeout;
    private int maxTotal;
    private int maxIdle;
    private int maxWaitMillis;
    private String minEvictableIdleTimeMillis;
    private int numTestsPerEvictionRun;
    private String timeBetweenEvictionRunsMillis;
    private Boolean testOnBorrow;
    private Boolean testWhileIdle;

}

修改过后的自动转配类:


import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @ClassName ServiceTestAutoConfigure
 * @Description ServiceTestAutoConfigure
 * @Author stack
 * @Version 1.0
 * @since 2019/6/26 22:26
 */
@Configuration
@ConditionalOnClass(RedisProperties.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisTestAutoConfigure {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    @ConditionalOnMissingBean(RedisConnectionFactory.class)
    public RedisConnectionFactory taskConnectionFactory() {
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPort(redisProperties.getPort());
        connectionFactory.setHostName(redisProperties.getHost());
        connectionFactory.setDatabase(redisProperties.getDb());
        connectionFactory.setPassword(redisProperties.getPass());

        //配置连接池属性
        connectionFactory.setTimeout(Integer.parseInt(redisProperties.getConfig().getTimeout()));
        connectionFactory.getPoolConfig().setMaxIdle(redisProperties.getConfig().getMaxIdle());
        connectionFactory.getPoolConfig().setMaxTotal(redisProperties.getConfig().getMaxTotal());
        connectionFactory.getPoolConfig().setMaxWaitMillis(redisProperties.getConfig().getMaxWaitMillis());
        connectionFactory.getPoolConfig().setMinEvictableIdleTimeMillis(Integer.parseInt(redisProperties.getConfig().getMinEvictableIdleTimeMillis()));
        connectionFactory.getPoolConfig().setNumTestsPerEvictionRun(redisProperties.getConfig().getNumTestsPerEvictionRun());
        connectionFactory.getPoolConfig().setTimeBetweenEvictionRunsMillis(Integer.parseInt(redisProperties.getConfig().getTimeBetweenEvictionRunsMillis()));
        connectionFactory.getPoolConfig().setTestOnBorrow(redisProperties.getConfig().getTestOnBorrow());
        connectionFactory.getPoolConfig().setTestWhileIdle(redisProperties.getConfig().getTestWhileIdle());

        return connectionFactory;
    }

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate redisTemplate() {
        RedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(taskConnectionFactory());
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(taskConnectionFactory());
        return template;
    }

}

这样跟源码就几乎一样了…
源码地址:
https://github.com/stackXu/study-authconfigure

世界上所有的不公平都是由于当事人能力不足造成的.
原文地址:https://www.cnblogs.com/javayida/p/13346912.html