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 ...
随机推荐
- Webpack 一些概念
目录 引子 Dependency Graph Runtime Manifest Module.Bundle.Chunk Bundle Splitting Code Splitting Tree Sha ...
- nginx和swoole怎么混合使用
有需要学习交流的友人请加入交流群的咱们一起,有问题一起交流,一起进步!前提是你是学技术的.感谢阅读! 点此加入该群jq.qq.com 基于epoll的Nginx 有了epoll,理论上1个进程就可以 ...
- openshift安装部署
前置准备工作: 1.每台主机准备好有公钥在 /root/.ssh/authorized_keys,私钥则存放在第一台主机的/root/.ssh/id_rsa 2.确定每台主机的私网IP地址是固定的. ...
- Nginx:The Location Block Selection Algorithm
Nginx:The Location Block Selection Algorithm,摘自NGINX:A PRACTICAL GUIDE TO HIGH PERFORMANCE Nginx配置文件 ...
- opengl画不出直线 线段 坐标轴 却能画出其他图形的坑
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/12054507.html 好多次都是画坐标轴的三条直线画不出来,虽然最后都解决了 但是还是耽误 ...
- 精通awk系列(6):awk命令结构和awk语法结构
回到: Linux系列文章 Shell系列文章 Awk系列文章 awk命令行结构和语法结构 awk命令行结构 awk [ -- ] program-text file ... (1) awk -f p ...
- pandas 初识(六)-可视化
Pandas 在一张图中绘制多条线 import pandas as pd import numpy as npimport matplotlib.pyplot as plt df = pd.Data ...
- How to: Generate XPO Business Classes for Existing Data Tables 如何:为现有数据表生成 XPO 业务类
From the Tutorial and other documentation sources, you learned how to create business classes for yo ...
- 易优CMS:channelartlist 获取当前频道的下级栏目的内容列表
channelartlist 获取当前频道的下级栏目的内容列表 [基础用法] 名称:channelartlist 功能:获取当前频道的下级栏目的内容列表标签 语法: {eyou:channelar ...
- node-sass 埋坑记录
node-sass 埋坑记录 背景 原有项目.环境: node:v8.16.2 npm:v6.4.1 node-sass::v4.8.0 Angular-CLI:v6.x 本机没有安装 Visual ...