当前位置:网站首页>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
边栏推荐
- C#异步和多线程
- 【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
- Concepts and Methods of Exploratory Testing
- Two ways to simulate multi-user login in Jmeter
- Odps temporary query can write SQL, turned out to a named?
- How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
- 2022暑假牛客多校联赛第一场
- Interface Test Framework Practice (4) | Get Schema Assertion
- 数字化时代,企业如何建立自身的云平台与商业模式的选择?
- DFS's complement to pruning
猜你喜欢
【生物素叠氮化物|cas:908007-17-0】价格_厂家
探索性测试的概念及方法
Shell conditional statement judgment
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
typescript43-类型兼容性说明
高可用 两地三中心
Where is the value of testers
【Harmony OS】【FAQ】鸿蒙问题合集1
力扣561. 数组拆分
Interface test practice | Detailed explanation of the difference between GET / POST requests
随机推荐
Concepts and Methods of Exploratory Testing
【 Harmony OS 】 【 ano UI 】 lightweight data storage
Shell之条件语句
typescript39-class类的可见修饰符
js中的闭包
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
JS bottom handwriting
Odps temporary query can write SQL, turned out to a named?
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
高可用 两地三中心
shell script loop statement
User password verification
Online password generator tool recommendation
1059 C语言竞赛 (20 分)(C语言)
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
[Fine talk] Using native js to implement todolist
Installation of Apache DolphinScheduler version 2.0.5 distributed cluster
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
用户密码验证
Interface testing framework of actual combat (2) | interface request assertion