IDEA提示找不到Mapper接口:Could not autowire.No beans of 'xxxMapper' type found
前言
相信大多数互联网公司的持久层框架都是使用 Mybatis 框架,而大家在 Service 层引入自己编写的 Mapper 接口时应该会遇到下面的情况:

我们可以看到,上面的红色警告在提示我们,找不到 xxxMaper 这个类型的 bean。
为啥呢?
因为 @Mapper 这个注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是却和 Mybatis 关联不上。而且我们可以根据 @Autowried 源码看到,默认情况下,@Autowried 要求依赖对象必须存在,那么此时 IDEA 只能给个红色警告了。
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to {@code true}.
*/
boolean required() default true;
}
怎么解决?
虽然 IDEA 给了个红色警告,但是程序本身是没问题的,可以正常运行。可是代码里头有红色警告是相当的显眼的,不晓得情况的人还以为我么你的代码有问题呢?
下面我将会列出实践过的解决方案。
1、简单粗暴
直接关掉IDEA的警告提示,是不是很简单?是不是很粗暴?
2、麻烦粗暴,给@Autowried注解设置required=false
@Autowired(required = false)
private CarUserMapper carUserMapper;
这样就不会警告了。因为此时 @Autowried 不会再去校验 Mapper 接口是否为空。
缺点:每引入一个Mapper接口都需要设置上required=false,相当的麻烦,而且容易给别人造成误解:这个Mapper接口真的不需一定存在。
3、@Autowried替换为@Resource
@Resource
private CarUserMapper carUserMapper;
此时也不会给红色警告了。@Resource 注解是 J2EE 提供的,而 @Autowried 注解是 Spring 提供的,他们如果感兴趣可以去看一下他们的区别。
4、在Mapper接口上加@Component
@Mapper
@Component
public inteface CarUserMapper{}
加这个注解呢,主要是为了让欺骗IEDA,让它以为CarUserMapper也是一个Spring管理的Bean,这样子使用@Autowired注解注入也不会报错了。
5、使用构造函数注入(Spring4.x推荐)
相信大家都有使用Lombok这个神器了,我们可以利用他的注解@RequiredArgsConstructor来直接构建构造函数即可,不过我尝试过单单使用@AllArgsConstructor也是没问题的。
当然了,大家如果注入的依赖比较少或者闲得蛋疼,还是可以自己来写构造函数的。
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
//@AllArgsConstructor
public class ChargeServiceImpl implements ChargeService {
private final RedisTemplate<String, Object> redisTemplate;
private final CarUserMapper carUserMapper;
private final ChargingMapper chargingMapper;
}
为什么Spring4.x之后就推荐使用构造函数注入呢?大家可以看看一篇文章:
Why field injection is evil
简单点总结一下就是:可以使依赖不可变、实例化时,会检查构造函数的参数是否为空、方便单元测试等等。
IDEA提示找不到Mapper接口:Could not autowire.No beans of 'xxxMapper' type found的更多相关文章
- SpringBoot注入Mapper提示Could not autowire. No beans of 'xxxMapper' type found错误
通过用Mabatis的逆向工程生成的Entity和Mapper.在Service层注入的时候一直提示Could not autowire. No beans of 'xxxMapper' type f ...
- IntelliJ Idea取消Could not autowire. No beans of 'xxxx' type found的错误提示
1.问题描述 在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示.但程序的编译和运行都是没有问题的, ...
- IntelliJ Idea解决Could not autowire. No beans of 'xxxx' type found的错误提示
本文转自:http://blog.csdn.net/u012453843/article/details/54906905 1.问题描述 在Idea的spring工程里,经常会遇到Could not ...
- 【转】IntelliJ Idea取消Could not autowire. No beans of 'xxxx' type found的错误提示
1.问题描述 在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示.但程序的编译和运行都是没有问题的, ...
- IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示
IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示哈,在使用 @Autowired 时,今天又遇一坑,这俩 ...
- IntelliJ Idea如何解决Could not autowire. No beans of 'xxxx' type found的错误提示
问题描述 在idea中进行开发时,经常会遇见Could not autowire. No beans of 'xxxx' type found的错误提示,这样的是不影响程序编译和运行的,但是看起来会很 ...
- 解决老是提示找不到Mapper文件无法执行定义的方法问题!
尼玛,被mybatis的*Mapper.xml文件害惨了!整整两天都在围绕这个问题转圈! 先看问题长啥样吧!下面是通过逆向工程生成的Mapper.xml文件,包路径什么的都没有错! 但是每次调用Map ...
- SpringBoot扫描包提示找不到mapper的问题
SpringBoot扫描包问题 报错信息:Consider defining a bean of type in your configuration 方法一: 使用注解 @ComponentScan ...
- IntelliJ IDEA中Mapper接口通过@Autowired注入报错的正确解决方式
转载请注明来源:四个空格 » IntelliJ IDEA中Mapper接口通过@Autowired注入报错的正确解决方式: 环境 ideaIU-2018.3.4.win: 错误提示: Could no ...
随机推荐
- Maven使用教程一:Maven基础
使用Maven快速创建一个工程 为了加快速度,在setting.xml中加一段配置,用国内阿里云的镜像仓库可以去下载各种东西. <mirror> <id>nexus-aliyu ...
- Android 状态栏通知 Notification
private NotificationManager manager; private Notification.Builder builder; @Override protected void ...
- 微信小程序—支付宝身份验证(支付宝小程序)
查看应用:https://open.alipay.com/platform/keyManage.htm 这里找到您调用接口的应用 支付宝身份验证快速接入:https://docs.open.alip ...
- Visual Studio 2019使用码云设置过滤忽略的文件或文件夹(ignore file)
Visual Studio 2019使用码云的时候,会遇到 “Git failed with a fatal error.error: open(".vs/{{项目名称}}/Server/s ...
- SpringCloud微服务实现生产者消费者+ribbon负载均衡
一.生产者springcloud_eureka_provider (1)目录展示 (2)导入依赖 <dependency> <groupId>org.springframewo ...
- SpringCloud 亿级流量 架构演进
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] 前言 Crazy ...
- Linux(三)
1.用户与用户组 Linux系统是一个多用户.多任务的操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员(root)申请一个账号,然后以这个账号的身份进入系统. ...
- 性能调优 -- Java编程中的性能优化
String作为我们使用最频繁的一种对象类型,其性能问题是最容易被忽略的.作为Java中重要的数据类型,是内存中占据空间比较大的一个对象.如何高效地使用字符串,可以帮助我们提升系统的整体性能. 现在, ...
- uml统一建模语言学习笔记(一)
UML是一种统一建模语言,他是以面向对象的方式来实现对任何的系统进行描述的一种语言, 它包括9种图形+包图,分为静态和动态两种,也就是结构图和行为图 “静态”图有:用例图.类图.对象图.部署图.构件图 ...
- arcgis api 4.x for js 自定义叠加图片图层实现地图叠加图片展示(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 4.x for js:esri 官网 api,里面详细的介绍 arcgis api 4.x 各个类 ...