解决springboot mapper注入报红问题
springboot mapper注入报红

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

好了,红线没了。

@Autowired自动注入时,mapper标红
问题:SpringBoot中,service层使用@Autowired自动注入时,mapper标红
为什么会标红?
我们使用的@Mapper和@MapperScan并不是spring框架的注解,未在spring容器中注册为bean;
解决方法
使用@Resource代替@Autowired注解,根据类型注入改为根据名称注入;
将注解@Autowired 改为(@Autowired(required = false)),表示注入时,自动注入的类为非必须;
@Autowired(required = false)
private UserMapper userMapper;
Setting -> Inspections -> 搜索spring bean,找到Spring Bean Autowired,把红色(Error)修改为黄色(Warning);
问题解决~~~
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring声明式事务注解之@EnableTransactionManagement解析
这篇文章主要介绍了Spring声明式事务注解之@EnableTransactionManagement解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-08-08
Springboot项目升级2.2.x升至2.7.x的示例代码
本文主要介绍了Springboot项目升级2.2.x升至2.7.x的示例代码,会有很多的坑,具有一定的参考价值,感兴趣的可以了解一下2023-09-09


最新评论