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的错误提示_第1张图片
AdminMapper类
在这里插入图片描述

2 解决方法

因为编译器提示无法自动注入,原因无非就是Spring IOC容器中没有或找不到这个组件

方法1:在mapper文件上加@Repository注解(我之前用的是@Mapper),这是从spring2.0新增的一个注解,用于简化 Spring 的开发,实现数据访问方法,如图
在这里插入图片描述
方法二:
在mapper文件上加@Component注解,把普通pojo实例化到spring容器中,相当于配置文件中的< bean id=“xxx” class=“Xxx” >,
在这里插入图片描述

补充:

1 如果是注入Service出现该问题,则可能是在该ServiceImpl实现类没有加@Service("")注解
2 使用@Mapper接口时不要忘记在SpringBoot的启动类加@MapperScan接口

你可能感兴趣的:(error,spring,spring,boot,java,mybatis,intellij,idea)