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 ...
随机推荐
- SpringBoot SpringCloud 热部署 热加载 热调试
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...
- ElasticSearch 7.4.2 Root mapping definition has unsupported parameters
新建索引 PUT people { "settings":{ "number_of_shards":3, "number_of_replicas&qu ...
- iOS 裁剪工具
下载 demo和工具下载链接SPClipTool 使用说明 [[SPClipTool shareClipTool] sp_clipOriginImage:pickerImage complete:^( ...
- Jsonp的js实现,跨域请求,同源策略机制
Jsonp的js实现,跨域请求,同源策略机制1.跨域请求:请求URL的协议,域名,端口三者之间任意一个与当前页面地址不同即为跨域 存在跨域的情况: 网络协议不同,端口不通,域名不同,子域名不同,域名和 ...
- OpenLDAP的docker版安装
分为服务器和client的web版. ldap.sh #!/bin/bash -e docker run --name ldap-service -- docker run --name phplda ...
- harbor部署之SSL
harbor部署之SSL 1 签名证书与自签名证书 签名证书:由权威颁发机构颁发给服务器或者个人用于证明自己身份的东西. 自签名证书:由服务器自己颁发给自己,用于证明自己身份的东西,非权威颁发机构发布 ...
- ROS--自定义消息类型
一.msg 用于发布-订阅的通信方式中. 1.在包的src 中创建msg文件夹. 2.在msg文件夹中,创建.msg文件 3.编辑.msg文件 4.编辑package.xml , 添加依赖 <b ...
- C#调试程序——断点+几种观察数据的方法
目录 C#调试程序--断点+观察数据的方法 1.写本文的背景 2.调试与测试 3.断点调试 3.1 F10 3.2 F11 3.3 SHIFT+F11 4.监视 4.1 按照1方法打断点,单步调试. ...
- web.xml的常见配置
web.xml的常见配置 <!-- 配置全局的编码过滤器 --> <filter> <description>编码过滤器</description> & ...
- js-02-循环语句
循环语句分类{ for while do ( ) while } 一.for循环语句和for循环的嵌套 for循环格式eg: <script> var sim = 0; for(var i ...