我不知道为什么会这样。 jedis 创建名称为 'jedisConnectionFactory' 的 bean 时出错

I do not know why this is happening. jedis Error creating bean with name 'jedisConnectionFactory'

运行 应用程序后检测到错误。 我找不到任何问题,我需要帮助。

包结构由配置和控制器组成。

spring-boot-starter-data-redis redis.clients 绝地武士 3.0.1

package com.arthur.springbootredis.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

@Configuration
public class RedisConfig {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {

        JedisConnectionFactory jedisConnectionFactory = null;

        try {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setDatabase(0);
            redisStandaloneConfiguration.setHostName("localhost");
            redisStandaloneConfiguration.setPort(6379);

            jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);

            jedisConnectionFactory.getPoolConfig().setMaxTotal(50);
            jedisConnectionFactory.getPoolConfig().setMaxIdle(50);
        } catch (RedisConnectionFailureException e) {
            e.getMessage();
        }

        return jedisConnectionFactory;
    }


    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        template.setEnableTransactionSupport(true);
        return template;
    }
}

错误内容如下

org.springframework.beans.factory.BeanCreationException:创建名称为 'jedisConnectionFactory' 的 bean 在 class 路径资源 [com/arthur/springbootredis/config/RedisConfig.class] 中定义时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]:工厂方法 'jedisConnectionFactory' 抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: redis/clients/util/SafeEncoder

感谢阅读。

您正在尝试使用 Jedis 3.0.x,它对 Jedis 2.x 提供的 API 进行了重大更改。 JedisConnectionFactory 抛出异常,它是 Spring Data Redis 的一部分,在撰写本文时,Spring Data Redis 仅支持 Jedis 2.x。支持 Jedis 3 has been implemented 但尚未发布。如果你想使用Spring Data Redis,你应该暂时坚持使用Jedis 2.x。对 Jedis 3.0 的支持将在 Spring Data Redis 2.2 中发布,它是 Spring Data Moore 发布系列的一部分,并将包含在 Spring Boot 2.2 中。

自 2019 年 7 月起,Spring Boot 2.X 与 Jedis 3.X 兼容(有关详细信息,请参阅 Upgrade to Jedis 3.1.0)。 对于那些使用Gradle的人,这是我的组合:

build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'redis.clients:jedis:3.1.0'
}