spring-data-redis 2.7.2报错Could not autowire. No beans of ‘RedisConnectionFactory‘ type found

报错界面:

 正确方式如下:

1.加入依赖

    <dependency>
	    <groupId>org.springframework.data</groupId>
	    <artifactId>spring-data-redis</artifactId>
	    <version>2.7.2</version>
    </dependency>
    <dependency>
	    <groupId>redis.clients</groupId>
	    <artifactId>jedis</artifactId>
	    <version>3.9.0</version>
    </dependency>

注意:这里2.7.2对应jedis最高只能到3.9.0版本,这个是通过多次尝试得来的经验。 

2. 配置

package com.cnwwj;

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;


@Configuration
public class RedisConfig {


    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        return new JedisConnectionFactory();
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

3.结果符合预期!


版权声明:本文为c5211314963原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。