记一个 @Resource BUG

严格来说并不是BUG,只是我对这个注解理解有误

场景: 项目中存在两个分支,需要我将第一个分支中的代码复制到第二个分支中,并将其中的controller,service,mapper单独复制一份并在类名前加上前缀

service中的bean的注入方式是@Resource,变量名就是类名的小写,我将mapper复制一份之后,service只更改了注入Bean的类名,没有修改bean的变量名,然后直接报错了

复制代码
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '一个Controller': Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '一个ServiceImpl': Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '一个ServiceImpl': Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '一个ServiceImpl': Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named '同下的Mapper' is expected to be of type '全类名.Mapper' but was actually of type 'com.sun.proxy.$Proxy341'

The bean 'xxxMapper' could not be injected as a '全类名.前缀XxxMapper' because it is a JDK dynamic proxy that implements:
	com.baomidou.mybatisplus.core.mapper.BaseMapper


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

甚至还报redis连接超时.....

我下意识的认为是mybatis中的xml写的有问题(这玩意出问题之后报错信息也挺奇怪的),于是一个个找,一个个看是哪个文件,捣鼓了两个小时也没找到啥问题,最后才发现是注入的问题

我只知道 @Resource 会先使用BeanName注入变量,被spring注册的Bean使用类名小写开头作为beanName,但是我不知道@Resource使用的BeanName是TM的变量名啊,这就导致使用变量名查找到的bean不能被注入到我复制出来的那个bean中,并且由于找到了bean还不会使用type来继续查找bean。。。这就导致查找的bean不能被注入到变量中导致报错,回过头来发现原本的报错信息好像挺准确的...

最后将注入方式改成了@Autowired就能正常启动了(或者把变量名改了)

相关推荐
天燹1 天前
Qt 6 嵌入 Android 原生应用完整教程
android·开发语言·qt
liu****1 天前
第一章 Qt 概述
开发语言·c++·qt
知行合一。。。1 天前
Python--04--数据容器(列表 List)
开发语言·python
J_liaty1 天前
Spring Security整合JWT与Redis实现权限认证
java·redis·spring·spring-security
程芯带你刷C语言简单算法题1 天前
Day48~对于高度为 n 的台阶,从下往上走,每一步的阶数为 1,2,3 中的一个。问要走到顶部一共有多少种走法
c语言·开发语言·学习·算法·c
csbysj20201 天前
SQL NOT NULL约束详解
开发语言
三角叶蕨1 天前
【苍穹外卖】day1
java
WAZYY06191 天前
通过LocalDateTime判断当前日期是否失效(附Java 中常用的 ISO 格式)
java·iso·日期·localdate·时间处理·日期处理·日期格式
低调小一1 天前
Kotlin 2025–2026 客户端开发路线:语言升级 × 跨端落地 × AI Agent 入门
开发语言·人工智能·kotlin
皙然1 天前
SpringBoot 自动装配深度解析:从底层原理到自定义 starter 实战(含源码断点调试)
java·spring boot·spring