一、問題由來
周日下午項目在進行測試時,有些東西需要臨時修改,自己已經打好一個包部署到測試服務器進行部署。在測試過程中發現一個問題,就是
現在的代碼跑起來是沒問題的,只是其他人又的東西還沒做,所以暫時修改一下代碼邏輯,讓測試能夠順利進行。自己根據需要立馬修改代碼,
而且在之前部署過一個版本后,自己也一直修改新的代碼,有些需求是剛定下來的。代碼修改好之后,使用idea進行clean、編譯、打包都沒問題,
可是在啟動后報了一個錯如標題所示。詳細的錯誤信息如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-10-31 15:40:12.035 [] [main] ERROR o.s.boot.SpringApplication[826] - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webLogAspect': Unsatisfied dependency expressed through field 'logInfoMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logInfoMapper' defined in file [D:\JavaWorkSpace\BigstuffParent\AppletsBackend\target\classes\applets\nature\mapper\LogInfoMapper.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [applets.nature.mapper.LogInfoMapper]: Specified class is an interface
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at applets.AppletsApplication.main(AppletsApplication.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logInfoMapper' defined in file [D:\JavaWorkSpace\BigstuffParent\AppletsBackend\target\classes\applets\nature\mapper\LogInfoMapper.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [applets.nature.mapper.LogInfoMapper]: Specified class is an interface
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 19 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [applets.nature.mapper.LogInfoMapper]: Specified class is an interface
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312)
... 30 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:13616', transport: 'socket'
關鍵錯誤信息是 Failed to instantiate [applets.nature.mapper.LogInfoMapper]: Specified class is an interface
翻譯過來大致意思就是實例化applets.nature.mapper.LogInfoMapper這個類的時候失敗了,指定的這個類applets.nature.mapper.LogInfoMapper是一個接口。
這就讓我納悶了,怎么會出現這種奇奇怪怪的問題呢?雖然還沒有正式發布,可是這個項目都已經跑了好幾個月了,難道出現神秘BUG了?
二、問題分析
自己把這個問題往搜索引擎里面一扔,立馬出現一大堆的結果。比如這篇博文的提供的信息,
根據文中博主遇到的問題來看,是有mapper.java接口文件重名,因此啟動的時候才出現這個問題。這給我提供了一個解決問題的思路,會不會我也出
現同樣的問題呢,帶着這樣的問題,我立馬開始尋找問題原因。
三、解決方案
找到一個解決問題的方向后,立馬開始排查,查看applets.nature.mapper.LogInfoMapper這個接口類是否有重名的。在項目當中進行反復搜索,都沒有
發現重名的,只有這一個接口。難道是博主的答案不對嘛?自己的項目中明明只有一個這一個接口啊。這個原因被自己暫時的排除,繼續排查原因。
排查方式二:
既然這個接口啟動時頻繁報錯,那我臨時把這個接口給注銷試一下,看能否啟動成功。當我把這個接口給注銷后,重新啟動發現還會報錯,不過不再是
報applets.nature.mapper.LogInfoMapper這個類未實例化,而是報的其他類不能實例化。錯誤信息如下:
Field userTaskMapper in applets.task.service.impl.UserTaskServiceImpl required a bean of type 'applets.task.mapper.UserTaskMapper' that could not be found.
我就納悶了,這到底是怎么回事呢?LogInfoMapper這個接口是最先被調用的,放在aop攔截器中用於記錄日志。把它注銷后,就報其他接口不能正常實例化。
排查方式三:
既然到這一步,我索性把代碼備份后,將修改后的代碼全部回滾到之前的狀態,重新啟動項目,啟動成功。然后逐步分析,是我加了什么東西還是修改了什么
代碼導致的。慢慢地分析我發現,當新添加一個mapper.java接口和xml文件后,導致項目不能正常啟動,如下圖:

去項目中進行搜索,果然發現有兩個同名的mapper接口

我立馬將新添加的mapper接口刪除,重新啟動項目,正常,問題解決。
從這里也學到一點,有時候分析問題不能光看問題本身,需要對問題進行深入的法分析。就像這個BUG,報錯的那個接口並沒有重復,
而是新添加的接口重復導致所有的mapper接口都不能正常實例化,不能實例化就不能正常注入類,導致后續的一系列問題。如果簡單
的看問題,是很難找到問題的真正的原因的,需要看到問題的本質才能夠更好的解決問題。
參考博文: https://www.cnblogs.com/yadongliang/p/7739546.html
