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

iaqfqrcu  于 12个月前  发布在  Redis
关注(0)|答案(3)|浏览(125)

运行应用程序后检测到错误。我找不到任何问题,我需要帮助。
软件包结构由配置和控制器组成。
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;
    }
}

字符串
下面是错误内容
[com/arthur/springbootredis/index/RedisConfig.class]:通过工厂方法示例化Bean失败;嵌套的异常是org. springframework.beans.BeanInstantiationException:未能示例化[org. springframework.data.redis.connection.jedis.JedisConnectionFactory]:工厂方法'jedisConnectionFactory'抛出异常;嵌套异常为java.lang.NoClassDefFoundError:redis/clients/util/SafeEncoder
感谢您的阅读。

falq053o

falq053o1#

您正在尝试使用Jedis 3.0.x,它对Jedis 2.x提供的API进行了突破性的更改。该异常由Spring Data Redis的一部分JedisConnectionFactory引发,在撰写本文时,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摩尔发布系列的一部分,并将包含在Sping Boot 2.2中。

vmpqdwk3

vmpqdwk32#

自2019年7月以来,Sping 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'
}

字符串

brtdzjyr

brtdzjyr3#

不同依赖版本的兼容性存在问题。
尝试将maven jedis依赖项设置为

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.10.0</version>
<type>jar</type>
</dependency>

字符串

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.7.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

相关问题