springboot mapper注入报红

在mapper接口上加上
@Component注解
例如:

好了,红线没了。
@Autowired自动注入时,mapper标红
问题:SpringBoot中,service层使用@Autowired自动注入时,mapper标红
为什么会标红?
我们使用的@Mapper和@MapperScan并不是spring框架的注解,未在spring容器中注册为bean;
解决方法
使用@Resource代替@Autowired注解,根据类型注入改为根据名称注入;
将注解@Autowired 改为(@Autowired(required = false)),表示注入时,自动注入的类为非必须;
|
1 2 |
|
Setting -> Inspections -> 搜索spring bean,找到Spring Bean Autowired,把红色(Error)修改为黄色(Warning);
问题解决~~~
本文介绍了解决SpringBoot项目中mapper接口注入失败的方法,包括使用@Resource替换@Autowired、设置@Autowired的required属性为false以及调整IDEA配置等方案。

3437

被折叠的 条评论
为什么被折叠?



