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项目

相关推荐
慌糖3 分钟前
Spring Boot音乐服务器项目-查询喜欢的音乐模块
服务器·spring boot·mybatis
love静思冥想2 小时前
MyBatis XML 配置方式是 返回 Boolean 类型
xml·mybatis
尚学教辅学习资料2 小时前
SpringBoot3.x入门到精通系列:1.2 开发环境搭建
spring boot·gradle·maven·intellij idea·jdk17·开发环境
运维小杨3 小时前
Redis主从复制搭建
数据库·redis·缓存
找不到、了3 小时前
Kafka在Springboot项目中的实践
spring boot·分布式·kafka
用户053140608815 小时前
Spring Boot AOP 切点表达式深度解析
spring boot
李长渊哦5 小时前
SpringBoot中ResponseEntity的使用详解
java·spring boot·后端
用户053140608815 小时前
Spring Boot AOP详解:优雅解耦,提升代码可维护性
spring boot
weixin_491853315 小时前
Spring Boot 中整合WebSocket
spring boot·后端·websocket
喵手5 小时前
Spring Boot 异常处理:从全局捕获到优化用户体验!
spring boot·python·ux