当前位置:网站首页>@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提供的注解
边栏推荐
- Arm pc=pc+8 is the most understandable explanation
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
- 数据分析之缺失值填充(重点讲解多重插值法Miceforest)
- A possible cause and solution of "stuck" main thread of RT thread
- Whistle+switchyomega configure web proxy
- Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
- Common properties of location
- Pytoch temperature prediction
- Mysqldump error1066 error solution
猜你喜欢
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
【ESP32学习-1】Arduino ESP32开发环境搭建
高通&MTK&麒麟 手機平臺USB3.0方案對比
基于Redis的分布式ID生成器
Understanding of AMBA, AHB, APB and Axi
[Red Treasure Book Notes simplified version] Chapter 12 BOM
ES6 grammar summary -- Part 2 (advanced part es6~es11)
AMBA、AHB、APB、AXI的理解
Variable star user module
Detailed explanation of Union [C language]
随机推荐
Priority inversion and deadlock
PT OSC deadlock analysis
小天才电话手表 Z3工作原理
Raspberry pie tap switch button to use
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Walk into WPF's drawing Bing Dwen Dwen
Types de variables JS et transformations de type communes
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
.elf .map .list .hex文件
MySQL占用内存过大解决方案
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
Pytorch-温度预测
Gallery's image browsing and component learning
Cannot change version of project facet Dynamic Web Module to 2.3.
Basic operations of databases and tables ----- creating data tables
Important methods of array and string
R & D thinking 01 ----- classic of embedded intelligent product development process
STM32 how to locate the code segment that causes hard fault
vim命令行笔记