当前位置:网站首页>@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提供的注解
边栏推荐
- uCOS-III 的特点、任务状态、启动
- C语言回调函数【C语言】
- Whistle+switchyomega configure web proxy
- Reno7 60W super flash charging architecture
- 列表的使用
- JS object and event learning notes
- Keyword inline (inline function) usage analysis [C language]
- Who says that PT online schema change does not lock the table, or deadlock
- Programmers can make mistakes. Basic pointers and arrays of C language
- Variable star user module
猜你喜欢

Pytorch four commonly used optimizer tests

【ESP32学习-1】Arduino ESP32开发环境搭建

Arm pc=pc+8 is the most understandable explanation

Kaggle competition two Sigma connect: rental listing inquiries

機器學習--線性回歸(sklearn)

sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer

小天才电话手表 Z3工作原理

Machine learning -- linear regression (sklearn)

Understanding of AMBA, AHB, APB and Axi

arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
随机推荐
基于Redis的分布式ID生成器
Bubble sort [C language]
电商数据分析--用户行为分析
[Red Treasure Book Notes simplified version] Chapter 12 BOM
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Basic operations of databases and tables ----- creating data tables
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
Types de variables JS et transformations de type communes
Unit test - unittest framework
E-commerce data analysis -- salary prediction (linear regression)
Apprentissage automatique - - régression linéaire (sklearn)
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
OPPO VOOC快充电路和协议
Vscode basic configuration
Kaggle competition two Sigma connect: rental listing inquiries
Embedded startup process
Reading notes of difficult career creation
GCC compilation options
I2C总线时序详解
Working principle of genius telephone watch Z3