解決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

