springboot集成redis问题:

在做springboot和redis 集成时,正常引入jar包,配置redis的配置文件,注入

@Autowired
private StringRedisTemplate stringRedisTemplate;

具体参看个人另一篇博客:springboot集成redis 

正常调用stringRedisTemplate的api,在启动springboot的过程中,启动失败,异常提升如下:

2019-07-30 23:59:37,219 DEBUG (LoggingFailureAnalysisReporter.java:38)- Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.StringRedisTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:543)
	at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java:40002)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41008)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
	at cn.quantgroup.promotion.PromotionApplication.main(PromotionApplication.java:32)
2019-07-30 23:59:37,220 ERROR (LoggingFailureAnalysisReporter.java:42)- 

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

Description:

Field stringRedisTemplate in cn.my.testcontroller.TestTableController required a bean of type 'org.springframework.data.redis.core.StringRedisTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.data.redis.core.StringRedisTemplate' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:57192', transport: 'socket'

Process finished with exit code 1

异常很明显:实例化StringRedisTemplate失败,找不到StringRedisTemplate

查看项目中注入的redisTemplate,发现could not autowire。

对于强迫症的人来讲,看到这个提示,首先很多人可能会认为这是idea的提示问题,由此设置idea的提示规则,忽略这种提示,以致于被误导,但问题并不能解决。

 

根据异常信息排查思路:

1、找不到StringRedisTemplate 实例,说明没有注入,一般的服务,没有注入可能是没对服务添加@Service注解,或者该服务没有被扫包扫描到,但是StringRedisTemplate 是有spring 自己管理注入的,实际却没被实例化注入,这就奇怪了

2、考虑有可能是redis的配置信息有误,导致创建和redis的连接失败,以至于实例化失败,但分析后发现,redis的配置信息是正确的,而且如果是配置信息错误,导致的连接失败,应该会报 连接redis的异常,而不是直接报StringRedisTemplate' that could not be found. 所以这种可能也不对

3、想跟进源码中查看,但是根据异常信息,粗略的跟进源码,但是也没最终确认问题

4、进行网上相似问题检索,有人说把@Autowired 注解替换为@Resource 即解决了问题,虽然认为@Autowired是基于类的类型type注入,而@Resource 是基于name 注入,替换应该起不了作用,但是还是试了一下,果不其然的问题依旧。

5、百思不得其解中,突然想到springboot 1.x 和springboot2.x 对一些组件的支持是和版本绑定的,而我创建的项目,springboot 的父pom引用的父类,被我替换为公司封装的 父类引用了,而公司引用的springboot 版本是1.x。 我引入的springboot redis的版本是2.x ,会不会是和这个有关系,报着试试的态度,修正redis 版本为1.x, 再次启动服务,问题解决

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值