Failed to instantiate [applets.nature.mapper.LogInfoMapper]: Specified class is an interface-项目启动报错
一、问题由来
周日下午项目在进行测试时,有些东西需要临时修改,自己已经打好一个包部署到测试服务器进行部署。在测试过程中发现一个问题,就是
现在的代码跑起来是没问题的,只是其他人又的东西还没做,所以暂时修改一下代码逻辑,让测试能够顺利进行。自己根据需要立马修改代码,
而且在之前部署过一个版本后,自己也一直修改新的代码,有些需求是刚定下来的。代码修改好之后,使用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