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

相关推荐
Flynt7 小时前
我把微信运动的排行榜和定点推送复刻了一遍:100万次步数更新只要4秒,坑在别处
redis·后端·性能优化
学长毕设7 小时前
基于SpringBoot的青少年编程学习平台系统-附源码
spring boot·学习·青少年编程
Song_RsHt12 小时前
Redis 与微服务,什么场景下无可替代
java·redis·后端·学习·微服务
学长毕设12 小时前
基于SpringBoot的建筑材料管理系统-附源码
java·spring boot·后端
星光开发者13 小时前
基于springboot智慧草场牛羊管理系统-计算机毕设【课程设计】50360
vue.js·spring boot·后端·mysql·php·课程设计·express
weixin_BYSJ198714 小时前
电竞先锋网站---附源码02032
java·vue.js·spring boot·python·django·tomcat·php
GottdesKrieges15 小时前
Redis哨兵模式新建用户和修改口令
数据库·redis
2601_9628857215 小时前
AkShare 和 Tushare 哪个好?区别、场景与选型
mybatis
她的男孩15 小时前
超时配置了自动驳回,任务状态却全变「已取消」:Forge Admin 流程超时终态与 Flowable ENTITY_DELETED
spring boot·低代码·工作流引擎
二宝哥16 小时前
02.springboot项目对接国内模型实现对话聊天
数据库·人工智能·spring boot