IntelliJ Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示

在使用 @Autowired 时,今天又遇一坑,这俩波浪线是干啥子嘛:

然鹅,试了一下,控制台也不报错,可以正常运行,

数据也有:

于是,又再百度上找答案。。

问题分析

在 Idea 的 spring 工程里,经常会遇到 Could not autowire. No beans of ‘xxxx’ type found 的错误提示。但程序的编译和运行都是没有问题的,这个错误提示并不会产生影响。但红色的错误提示在有些有强迫症的程序员眼里,多多少少有些不太舒服。

问题原因其一

第一个是 Intellij IDEA 本身工具的问题。IDEA 可以理解 Spring 的上下文,这个是因为 它是 MyBatis 的接口,不认识

这个一共有 7 种解决方案

解决办法:

(1)在注解上加上:

@Autowired(required = false)
  • 1

(2)使用 @Resource 替换 @Autowired

(3)在 MyBatis 接口上加上 @Repository 注解

(4)使用 Lombok,@RequireArgsContructor

(5)降低 Autowired 检测的级别,将 Severity 的级别由之前的 error 改成 warning 或其它可以忽略的级别。

(6)安装 MyBatis 插件

(7)构造器注入

原文地址:https://www.cnblogs.com/kaola8023/p/14143215.html