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);
問題解決~~~
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/Sir_He/article/details/81879854