Specified class is an interface

本文分析了一个Spring Boot应用中遇到的Bean创建异常,异常信息提示指定的类是接口。问题根源在于两个Mapper接口重名,通过重命名相关类并重启应用解决了问题。进一步探讨了源码中抛出此异常的原因,提醒开发者在排查此类问题时要关注类的实例化和依赖注入过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Specified class is an interface  指定的类是接口

 

异常错误信息

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxx.xxxMapper]: Specified class is an interface
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1294) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	... 44 common frames omitted

在往上找堆栈信息

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'analysisService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxMapper' defined in file [D:\workspaces\y_R\cloud\analysis-receive-service\target\classes\com\xxx\mapping\XxxMapper.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxs.mapping.XxxMapper]: Specified class is an interface
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:325) ~[spring-context-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.14.RELEASE.jar:5.1.14.RELEASE]

每个段异常信息里都有 Specified class is an interface  翻译过来就是:指定的类是接口

网上百度找了下,大多是说什么Cntroller里的API接口没有加 @RequestParam注解,可以说 加了也不好使。

又好好读了一遍异常信息,发现是两个Mapper重名导致的。

解决步骤

找到重名的类,rename--> 重启 --> 运行成功

这就完了吗?如果你觉得这就完了,那你就可能永远停留在此!!!

 

源码分析

抛出异常的原因是什么?

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值