【学习之路】spring boot 整合redis 报错

本文介绍了在Spring Boot项目中整合Redis时遇到的问题,包括报错信息和之前采用的错误配置。通过分享解决方法,帮助读者理解并避免此类问题,同时也欢迎读者留言交流更好的实践建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 踩坑原因

报错内容:

2022-04-27 17:34:24.004  WARN 2159230 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisController' defined in URL [jar:file:/usr/local/sbin/demo-web-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/dx/demoweb/controller/RedisController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate
2022-04-27 17:34:24.067  INFO 2159230 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-04-27 17:34:24.102  INFO 2159230 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-27 17:34:24.148 ERROR 2159230 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.dx.demoweb.controller.RedisController required a single bean, but 2 were found:
        - redisTemplate: defined by method 'redisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]
        - stringRedisTemplate: defined by method 'stringRedisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

2022-04-27 17:34:25.085  WARN 2159230 --- [       Thread-3] c.a.n.common.http.HttpClientBeanHolder   : [HttpClientBeanHolder] Start destroying common HttpClient
2022-04-27 17:34:25.086  WARN 2159230 --- [       Thread-3] c.a.n.common.http.HttpClientBeanHolder   : [HttpClientBeanHolder] Destruction of the end

二. 踩坑之前

我之前用的构造函数的方式注入


    private RedisTemplate redisTemplate;

    @Autowired
    RedisController(RedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;
    }

二. 解决方法

我的解决方式:

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

PS

希望对看到本文的人有所帮助,如果你有更好的建议或思路欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值