当前位置:网站首页>idea uses @Autowired annotation to explain the reasons and solutions
idea uses @Autowired annotation to explain the reasons and solutions
2022-08-03 05:12:00 【strange elephant】
When using idea's @Autowired annotation to inject Mapper files, sometimes it will be marked in red, but it does not affect the operation of the project, so what is the reason?
reason
First look at the @Autowired source code:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Autowired {/*** Declares whether the annotated dependency is required.* Defaults to {@code true}.*/boolean required() default true;
The default value is true, indicating that the @Autowired annotation must inject a non-empty object. The package of the @Autowired annotation comes from:
import org.springframework.beans.factory.annotation.Autowired;
@Mapper's package comes from:
import org.apache.ibatis.annotations.Mapper;
Summary of the reasons for the error: The @Autowired annotation requires a non-empty object, and @Autowired is of SpringFramework, but the object it introduces is the @Mapper annotation of Mybatis, and idea cannot recognize the annotation of Mybatis well, so idea cannot confirm whether the currently injected object is a non-NULL object.
Extended: If the default value of the @Autowired annotation is set to false, no error will be reported
Solution
1). Use the @Resource annotation, which is a java annotation;
2). Turn off the error in Autowired in the idea setting:
3). Add a SpringFramework annotation to the mapper layer, such as @Repository annotation:
@[email protected] interface UserMapper {}
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 在树莓派上搭建属于自己的网页(2)
- Super handy drawing tool is recommended
- DFS对剪枝的补充
- BIOTIN ALKYNE CAS:773888-45-2价格,供应商
- Harmony OS Date ano UI 】 【 】 the basic operation
- Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
- Secondary development of WinForm controls
- 2022/08/02 学习笔记 (day22) 多线程
- Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
- shell script loop statement
猜你喜欢
1. 两数之和
Kotlin-Flow常用封装类:StateFlow的使用
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
MCM箱模型建模方法及大气O3来源解析
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
Online password generator tool recommendation
High availability, two locations and three centers
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
接口测试如何准备测试数据
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
随机推荐
Shell之条件语句
接口和抽象
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
数字化时代,企业如何建立自身的云平台与商业模式的选择?
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
FileZilla 搭建ftp服务器
力扣561. 数组拆分
Flink状态
Online password generator tool recommendation
PotPlayer实现上班摸鱼电视自由
业务表解析-余额系统
【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始
typescript41-class类的私有修饰符
DFS对剪枝的补充
【Harmony OS】【ARK UI】Date 基本操作
js中的闭包
typescript46-函数之间的类型兼容性
2. 两数相加
3. 无重复字符的最长子串