当前位置:网站首页>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
边栏推荐
- 【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
- Bubble sort in c language structure
- typescript41-class类的私有修饰符
- Flink state
- 常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
- Interface test framework combat (1) | Requests and interface request construction
- Online password generator tool recommendation
- 接口测试如何准备测试数据
- JS bottom handwriting
- 3. 无重复字符的最长子串
猜你喜欢

idea使用@Autowired注解爆红原因及解决方法

【 Harmony OS 】 【 ano UI 】 lightweight data storage

2022/08/02 学习笔记 (day22) 多线程

【Harmony OS】【FAQ】Hongmeng Questions Collection 1

Coordinate knowledge in digital twin campus scenarios

阿里云对象存储oss私有桶生成链接

接口测试如何准备测试数据

Install PostgreSQL on Windows

【生物素叠氮化物|cas:908007-17-0】价格_厂家

Harmony OS Date ano UI 】 【 】 the basic operation
随机推荐
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
数字化时代,企业如何建立自身的云平台与商业模式的选择?
私域流量时代来临,电商企业如何布局?
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
1069 微博转发抽奖 (20 分)(C语言)
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
接口和抽象
Apache DolphinScheduler版本2.0.5分布式集群的安装
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
【Harmony OS】【ARK UI】轻量级数据存储
获取Ip工具类
Shell conditional statement judgment
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
[Harmony OS] [ArkUI] ets development graphics and animation drawing
Talking about GIS Data (6) - Projected Coordinate System
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
IO process thread -> thread -> day5