我不知道为什么会发生这种情况。jedis 创建名为“jedisConnectionFactory”的 bean 时出错

최혜민*_*최혜민 2 redis jedis spring-boot

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

包结构由config和controller组成。

spring-boot-starter-data-redis redis.clients jedis 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是错误内容

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

感谢您的阅读。

And*_*son 5

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