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 分钟前
MyBatis:注解开发全攻略 - 从 XML 迁移到混合模式最佳实践
mybatis
资深web全栈开发1 小时前
如何正确使用缓存:常见陷阱与最佳实践
redis·缓存·golang
235162 小时前
【JVM】Java为啥能跨平台?JDK/JRE/JVM的关系?
java·开发语言·jvm·spring boot·后端·spring·职场和发展
Craaaayon3 小时前
如何选择两种缓存更新策略(写缓存+异步写库;写数据库+异步更新缓存)
java·数据库·redis·后端·缓存·mybatis
一 乐3 小时前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·小程序·论文
foxbillcsdn5 小时前
《Redis应用实例》Java实现(28):栈
java·redis
程序定小飞7 小时前
基于springboot的作业管理系统设计与实现
java·开发语言·spring boot·后端·spring
星辰_mya7 小时前
亲爱的redis你好
redis
qq_12498707539 小时前
基于springboot+vue的物流管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
刘一说10 小时前
深入理解 Spring Boot Actuator:构建可观测性与运维友好的应用
运维·spring boot·后端