当前位置:网站首页>idea使用@Autowired注解爆红原因及解决方法
idea使用@Autowired注解爆红原因及解决方法
2022-08-03 04:46:00 【奇怪的大象】
在使用idea的@Autowired注解注入Mapper文件时,有时会标红,但是不影响项目运行,那么是什么原因呢
原因
先看一下@Autowired源码:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to {@code true}.
*/
boolean required() default true;
默认值为true说明@Autowired注解必须要注入一个非空的对象,@Autowired注解的包,来自于:
import org.springframework.beans.factory.annotation.Autowired;
@Mapper的包来自于:
import org.apache.ibatis.annotations.Mapper;
总结报错的原因:@Autowired注解需要一个非空的对象,而@Autowired是SpringFramework的,但是它引入的对象是Mybatis的@Mapper注解,而idea不能很好地识别Mybatis的注解,所以idea不能确认当前要注入的对象是否为非NULL对象。
引申:如果把@Autowired注解的默认值设为false也不会报错
解决方案
1).使用@Resource注解,这是一个java注解;
2).在idea的setting里把Autowired中error关了:
3).在mapper层添加上一个SpringFramework的注解,比如@Repository注解:
@Mapper
@Repository
public interface UserMapper {
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- typescript39-class类的可见修饰符
- 2.何为张量
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- 社交电商:流量红利已尽,裂变营销是最低成本的获客之道
- Interface testing framework combat (3) | JSON request and response assertion
- typescript41-class类的私有修饰符
- Kotlin-Flow常用封装类:StateFlow的使用
- [Fine talk] Using native js to implement todolist
- 2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
- 8.电影评论分类:二分类问题
猜你喜欢
t conditional judgment statement and if loop
Harmony OS Date ano UI 】 【 】 the basic operation
Kotlin-Flow常用封装类:StateFlow的使用
MySQL 入门:Case 语句很好用
Windows 安装PostgreSQL
shell脚本循环语句
接口测试如何准备测试数据
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
Shell条件语句判断
随机推荐
MySQL 出现 The table is full 的解决方法
9.新闻分类:多分类问题
shell script loop statement
DDL操作数据库、表、列
在线密码生成工具推荐
荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
[Harmony OS] [ArkUI] ets development graphics and animation drawing
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
用户密码加密工具
Talking about GIS Data (6) - Projected Coordinate System
shell脚本循环语句
t conditional judgment statement and if loop
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
DFS对剪枝的补充
RequestContextHolder
建立树形结构
7.Keras开发简介
Interface test Mock combat (2) | Combined with jq to complete batch manual Mock
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
私域流量引流方法?分享购火爆的商业模式,你值得拥有