当前位置:网站首页>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
边栏推荐
- 1094 谷歌的招聘 (20 分)
- 1079 延迟的回文数 (20 分)
- Interface test Mock combat (2) | Combined with jq to complete batch manual Mock
- 2022/08/02 学习笔记 (day22) 多线程
- typescript40-class类的保护修饰符
- Installation of Apache DolphinScheduler version 2.0.5 distributed cluster
- MCM box model modeling method and source analysis of atmospheric O3
- 接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
- Odps temporary query can write SQL, turned out to a named?
- Online password generator tool recommendation
猜你喜欢

在线密码生成工具推荐

【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接

typescript43-类型兼容性说明

Coordinate knowledge in digital twin campus scenarios

【Harmony OS】【ARK UI】轻量级数据存储

Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)

数字孪生园区场景中的坐标知识

Jmeter 模拟多用户登录的两种方法

在树莓派上搭建属于自己的网页(2)

Talking about GIS Data (5) - Geographic Coordinate System
随机推荐
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
WinForm的控件二次开发
Odps temporary query can write SQL, turned out to a named?
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
2022暑假牛客多校联赛第一场
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
MCM box model modeling method and source analysis of atmospheric O3
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
在线密码生成工具推荐
私域流量引流方法?分享购火爆的商业模式,你值得拥有
shell脚本循环语句
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
Create a tree structure
DFS's complement to pruning
Talking about GIS Data (6) - Projected Coordinate System
Super handy drawing tool is recommended
Harmony OS Date ano UI 】 【 】 the basic operation
【开发者必看】【push kit】推送服务服务典型问题合集2
接口和协议
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?