一 、 场景
IDEA写了个接口,在Service服务实现类ServiceImpl中注入Mapper时,报错:Could not autowire. No beans of 'SysMenuMapper' type found ,但项目可以正常运行,结果也正确。

二、问题原因
原因在于DAO接口添加的@Mapper并不是Spring的注解,而是ibatis的注解,Spring auto scan配置找不到对应的bean,并没有声明这个DAO接口作为Spring的Bean,因此Spring不能进行管理,导致注入报错。
三、解决办法
方法一
只需在DAO接口加上@Component 或者 @Repository(@Repository实际上也包含了@Component注解)注解声明为Spring的Bean即可。

效果

方法二
将@Autowrited改为@Autowrited(required = false)
效果

文章持续更新中、希望对各位有所帮助、有问题可留言 大家共同学习 !
网友评论