springboot集成redis問題:

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

@Autowired
private StringRedisTemplate stringRedisTemplate;

正常調用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, 再次啓動服務,問題解決

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章