spring boot 引入redis报错

bug描述

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetChangeOrderController': Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [com/rdl/rterp/config/RedisConfig.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'redisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoder

解决办法:

引入正确的jedis依赖版本

xml 复制代码
<!--        redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.7.0</version> <!-- 使用适合你的项目的版本 -->
        </dependency>

yaml配置文件:

yaml 复制代码
  #连接的spring.redis
  redis:
    port: 6379
    host: 172.16.1.254
    timeout: 5000
    password:
    database: 14
    jedis:
      pool:
        max-active: 10
        max-wait: -1ms
        max-idle: 8                         #空闲
        min-idle: 0
    client-type: jedis

config包RedisConfig 配置文件

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

最后成功启动spring boot项目

相关推荐
苹果醋324 分钟前
Vue3响应式数据: 深入分析Ref与Reactive
java·运维·spring boot·mysql·nginx
bohu8339 分钟前
快速搭建springcloud 3.X+mybatis+nacos本地项目
spring cloud·nacos·mybatis
it噩梦1 小时前
springboot 工程使用proguard混淆
java·spring boot·后端
m0_748254882 小时前
Spring Boot实现多数据源连接和切换
spring boot·后端·oracle
tatasix2 小时前
Go Redis实现排行榜
开发语言·redis·golang
2401_850410833 小时前
redis的持久化
数据库·redis·bootstrap
Java雪荷3 小时前
基于 Vant UI + Redisson BitSet 实现签到日历
java·redis·vue
xmh-sxh-13143 小时前
redis相关数据类型介绍
redis
MySheep.4 小时前
爬虫数据存储:Redis、MySQL 与 MongoDB 的对比与实践
redis·爬虫·mysql
随猿Fa5 小时前
arthas查看拼接好参数的sql, redis, es完整可直接执行的命令
redis·sql·elasticsearch