当前位置:网站首页>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
边栏推荐
- Practical application of WebSocket
- OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
- 1059 C语言竞赛 (20 分)(C语言)
- UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
- Interface testing framework of actual combat (2) | interface request assertion
- MOSN 反向通道详解
- tag单调栈-单调栈预备知识-lt.739. 每日温度
- 2022暑假牛客多校联赛第一场
- 多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
- WinForm的控件二次开发
猜你喜欢
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
Shell条件语句判断
IO进程线程->线程->day5
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
Two ways to simulate multi-user login in Jmeter
Concepts and Methods of Exploratory Testing
接口测试实战| GET/POST 请求区别详解
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
随机推荐
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
Windows 安装PostgreSQL
GIS数据漫谈(六)— 投影坐标系统
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
MCM箱模型建模方法及大气O3来源解析
【精讲】利用原生js实现todolist
Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
Shell条件语句判断
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
接口和抽象
【生物素叠氮化物|cas:908007-17-0】价格_厂家
Interface test practice | Detailed explanation of the difference between GET / POST requests
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
Detailed explanation of MOSN reverse channel
建立树形结构
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)
1079 延迟的回文数 (20 分)