当前位置:网站首页>@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 operations of databases and tables ----- creating data tables
- Analysis of charging architecture of glory magic 3pro
- ARM PC=PC+8 最便于理解的阐述
- Arduino JSON data information parsing
- Reno7 60W超级闪充充电架构
- 数据分析之缺失值填充(重点讲解多重插值法Miceforest)
- Important methods of array and string
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- vim命令行笔记
- The first simple case of GNN: Cora classification
猜你喜欢

ES6语法总结--上篇(基础篇)

Arm pc=pc+8 is the most understandable explanation

open-mmlab labelImg mmdetection

Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights

Working principle of genius telephone watch Z3

高通&MTK&麒麟 手機平臺USB3.0方案對比

AMBA、AHB、APB、AXI的理解

Kconfig Kbuild

Walk into WPF's drawing Bing Dwen Dwen

Cannot change version of project facet Dynamic Web Module to 2.3.
随机推荐
Arduino JSON data information parsing
Reading notes of difficult career creation
Pytoch implements simple linear regression demo
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Oppo vooc fast charging circuit and protocol
Detailed explanation of Union [C language]
Whistle+switchyomega configure web proxy
Machine learning -- decision tree (sklearn)
Arduino uno R3 register writing method (1) -- pin level state change
Who says that PT online schema change does not lock the table, or deadlock
几个关于指针的声明【C语言】
Important methods of array and string
Basic operations of databases and tables ----- classification of data
机器学习--决策树(sklearn)
ESP learning problem record
【ESP32学习-2】esp32地址映射
ES6语法总结--上篇(基础篇)
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation