springboot中使用Redis时的报错解决方案

文章讲述了在使用SpringBoot时遇到的Redis连接问题,报错源于可能的依赖冲突,特别是netty版本不一致或者缺少redis连接池依赖commons-pool2。解决方案包括检查lettuce-core中的netty版本并统一,以及确保引入正确的连接池依赖及其对应版本。
摘要由CSDN通过智能技术生成
  1. 报错信息:

    sh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'stringRedisTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stringRedisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'stringRedisTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Unsatisfied dependency expressed through method 'redisConnectionFactory' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lettuceClientResources' defined in class path resource

    • 这里是Controller类报错,不过异常都是一层一层往外抛的,所以一直往下看,会发现是 ‘lettuceClientResources’ 出错了, 如果你的redis连接配置确保正确的话,有可能是你同时引入了netty的依赖,因为 spring-boot-starter-data-redis 中包含 lettuce-core依赖,而 lettuce-core 中又包含一些netty的依赖, 所以,如果你引入的netty依赖和lettuce-core中的不一样,就会出错。

    • 解决: 查看lettuce-core中的netty版本,将自己的netty版本改成一样的。

      具体做法:点击spring-boot-starter-data-redis依赖查找lettuce-core,再点进lettuce-core找netty,就可以看到版本号了

  2. 如果最后是 ‘redisConnectionFactory’ 报错, 那可能是连接池依赖没有引入, 需要引入该依赖:commons-pool2 ,且版本要对应,不然也会报错(不写版本号,用springboot管理的version就好)。

    <!--ommon-pool-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
    

a

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值