@Autowired Could not autowire
1 简介
在使用Intellij IDEA时,springboot整合mybatis在IndexController类中有如下的代码:
public class IndexController {
@Autowired
private LeaveMapper;
}
用@Autowire注解完成属性装配
@Autowire注解和@Resource一样,同样也可以标注在字段或属性的setter方法上,但它默认按类型装配。但是在添加了@Autowired注解之后,private LeaveMapper leaveMapper;下面就标识了红色的波浪线提示错误了,该如何解决该问题呢?
2 问题描述
通过点击红色leaveMapper,可以得到如下的提示:
Could not autowire. No beans of ‘LeaveMapper’ type found.(Ctrl + F1)
Checks autowiring problems in a bean class.
之前通过查询网上,查找解决方案,找到一种说法:即在程序结构中,可以通过快捷键Ctrl + Alt + Shift + S,一键到达,然后点击Facets移除Spring检测。但这种方式其实是有问题的,不建议采用
3 解决方式
点击IntelliJ IDEA设置[Ctrl + Alt + S],直接在搜索栏中搜索autowiring for Bean Class,直接去掉该项勾选即可。
逐级查找如下:
File—>settings—>Editor—>Inspections—>Spring—>Spring Core—>Code—>Autowiring for Bean Class
去掉勾选项,然后选择Apply和OK确认即可解决leaveMapper波浪线问题。