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 ...
随机推荐
- 关于腾讯云Centos的一些操作
安装mysql wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...
- C#线程学习笔记六:线程同步--信号量和互斥体
本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/23/Mutex_And_Semaphore.html,记录一下学习过程以备后续查用. ...
- 简单介绍托管执行和 CLI
目录 CIL 和 ILDASM 查看 myApp.dll 的 CIL 输出 使用 ILSpy 查看 myApp.dll 反编译后的代码 处理器不能直接解释程序集.程序集用的是另一种语言,即公共中间语言 ...
- 使用CocoaPods配置iOS百度地图sdk问题记录20191024
1.在Podfile中加入添加库名 pod 'BaiduMapKit' #百度地图SDK 2.安装百度地图 pod install 出现问题: [!] Error installing BaiduMa ...
- bayaim——docker.txt
#菜鸟教程地址https://www.runoob.com/docker/docker-tutorial.html#docker官方地址仓库https://hub.docker.com/ ------ ...
- oracle性能优化(项目中的一个sql优化的简单记录)
在项目中,写的sql主要以查询为主,但是数据量一大,就会突出sql性能优化的重要性.其实在数据量2000W以内,可以考虑索引,但超过2000W了,就要考虑分库分表这些了.本文主要记录在实际项目中,一个 ...
- CodeForces - 5C(思维+括号匹配)
题意 https://vjudge.net/problem/CodeForces-5C 给出一个括号序列,求出最长合法子串和它的数量. 合法的定义:这个序列中左右括号匹配. 思路 这个题和普通的括号匹 ...
- Hadoop入门学习笔记总结系列文章导航
一.为何要学习Hadoop? 这是一个信息爆炸的时代.经过数十年的积累,很多企业都聚集了大量的数据.这些数据也是企业的核心财富之一,怎样从累积的数据里寻找价值,变废为宝炼数成金成为当务之急.但数据增长 ...
- canvas的介绍
1.我们前端的绘图技术有哪些: 统计图表:echarts 实时走势图:canvas: 在线画板:魔猴: HTML5游戏:three.js 2.我这里主要讲的是canvas绘图: <canvas& ...
- js 日期格式化小问题
看一个图 toLocaleString 的格式是 yyyy/MM/d , 想要 yyyy/MM/dd, toISOString 的格式基本满足, 本想直接 split("T"), ...