当前位置:网站首页>idea使用@Autowired注解爆红原因及解决方法
idea使用@Autowired注解爆红原因及解决方法
2022-08-04 05:36:00 【m0_67402588】
在使用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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 零分贝超静音无线鼠标!数量有限!!先到先得!!!【元旦专享】
- Online public account article content to audio file practical gadget
- Jenkins pipeline 自动部署实践
- MAML原理讲解和代码实现
- Computer knowledge: desktop computers should choose the brand and assembly, worthy of collection
- 类图规范总结
- 缓动动画,有关窗口的一些常见操作,BOM操作
- 第九篇 ApplicationContext初始化
- Gramm Angle field GAF time-series data into the image and applied to the fault diagnosis
- 狗都能看懂的Self-Attention讲解
猜你喜欢

如何画好业务架构图。

用手机也能轻松玩转MATLAB编程

狗都能看懂的CenterNet讲解及代码复现

狗都能看懂的变化检测网络Siam-NestedUNet讲解——解决工业检测的痛点

Nacos 原理

Interpretation of EfficientNet: Composite scaling method of neural network (based on tf-Kersa reproduction code)

基于EEMD+GRU+MLR的时间序列预测

类图规范总结

Microsoft Store 微软应用商店无法连接网络,错误代码:0x80131500

RuntimeError: You called this URL via POST, but the URL doesn‘t end in a slash and you have APPEND_S
随机推荐
Visualization and Animation Technology (VR System)
SENet详解及Keras复现代码
如何用matlab做高精度计算?【第二辑】
DropBlock: 卷积层的正则化方法及复现代码
数据库:整理四个实用的SQLServer脚本函数
mysql月份比較是否相等
Time Series Forecasting Based on Reptile Search RSA Optimized LSTM
水平垂直居中的12种方法,任意插入节点的方法,事件的绑定的三种方法和解绑的方法,事件对象,盒子模型
Computer knowledge: desktop computers should choose the brand and assembly, worthy of collection
用matlab打造的摩斯电码加解码器音频版,支持包括中文在内的任意字符
YOLOv3详解:从零开始搭建YOLOv3网络
如何用matlab做高精度计算?【第三辑】(完)
网络技巧:教你给路由器装上电池,断电照样可以上网!
电脑知识:台式电脑应该选择品牌和组装,值得收藏
狗都能看懂的Self-Attention讲解
U-Net详解:为什么它适合做医学图像分割?(基于tf-Kersa复现代码)
【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
自适应迁移学习核极限学习机用于预测
异步编程之promise,任务队列,事件循环
CMDB 阿里云部分实现