当前位置:网站首页>@Autowired 和 @Resource 的区别
@Autowired 和 @Resource 的区别
2022-07-06 09:17:00 【阿杆.】
@Autowired 和 @Resource 的区别
默认注入方式不同
@Autowired 默认的注入方式为byType(根据类型进行匹配),也就是说会优先根据接口类型去匹配并注入 Bean (接口的实现类),如果想要指定名称,可以通过@Qualifier配合使用。
@Autowired
private UserSafetyMapper userSafetyMapper;
// 或
@Autowired @Qualifier("userSafetyMapper")
private UserSafetyMapper userSafetyMapper;
@Resource 默认注入方式为 byName(根据命名进行匹配)。如果无法通过名称匹配到对应的实现类的话,注入方式会变为byType。
- 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常;
- 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常;
- 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常;
- 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。
// 1.默认注入方式
@Resource
private UserSafetyMapper userSafetyMapper;
// 2.指定注入方式
@Resource(name = "userSafetyMapper", type = UserSafetyMapper.class)
private UserSafetyMapper userSafetyMapper;
提供者不同
@Autowired 是spring提供的注解,@Resource 是JDK提供的注解
边栏推荐
- Basic knowledge of lithium battery
- JS object and event learning notes
- C语言函数之可变参数原理:va_start、va_arg及va_end
- Whistle+switchyomega configure web proxy
- I2C bus timing explanation
- Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
- Kaggle competition two Sigma connect: rental listing inquiries
- Dependency in dependencymanagement cannot be downloaded and red is reported
- 关键字 inline (内联函数)用法解析【C语言】
- 小天才电话手表 Z3工作原理
猜你喜欢
Basic operations of databases and tables ----- modifying data tables
JS数组常用方法的分类、理解和运用
MySQL占用内存过大解决方案
电商数据分析--薪资预测(线性回归)
基于Redis的分布式ID生成器
Machine learning -- linear regression (sklearn)
【ESP32学习-2】esp32地址映射
JS正则表达式基础知识学习
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
随机推荐
gcc 编译选项
List and set
Embedded startup process
Time slice polling scheduling of RT thread threads
Arduino get random number
Rough analysis of map file
arduino获取数组的长度
Mysqldump error1066 error solution
機器學習--線性回歸(sklearn)
Symbolic representation of functions in deep learning papers
Cannot change version of project facet Dynamic Web Module to 2.3.
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Characteristics, task status and startup of UCOS III
1081 rational sum (20 points) points add up to total points
C语言函数之可变参数原理:va_start、va_arg及va_end
小天才电话手表 Z3工作原理
.elf .map .list .hex文件
JS 函数提升和var变量的声明提升
The first simple case of GNN: Cora classification
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE