一. 踩坑原因
报错内容:
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
希望对看到本文的人有所帮助,如果你有更好的建议或思路欢迎留言。