当前位置:网站首页>@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提供的注解
边栏推荐
- Kaggle competition two Sigma connect: rental listing inquiries
- The first simple case of GNN: Cora classification
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- The dolphin scheduler remotely executes shell scripts through the expect command
- .elf .map .list .hex文件
- [template] KMP string matching
- Who says that PT online schema change does not lock the table, or deadlock
- JS 函数提升和var变量的声明提升
猜你喜欢

Basic operations of databases and tables ----- creating data tables

几个关于指针的声明【C语言】

Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0

STM32 how to locate the code segment that causes hard fault

Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍

Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect

Basic operations of databases and tables ----- modifying data tables

高通&MTK&麒麟 手机平台USB3.0方案对比

Kconfig Kbuild

数据分析之缺失值填充(重点讲解多重插值法Miceforest)
随机推荐
ESP8266通过arduino IED连接巴法云(TCP创客云)
Detailed explanation of 5g working principle (explanation & illustration)
GCC compilation options
关键字 inline (内联函数)用法解析【C语言】
STM32 how to locate the code segment that causes hard fault
ESP8266使用arduino连接阿里云物联网
RT-Thread 线程的时间片轮询调度
Gallery's image browsing and component learning
Bubble sort [C language]
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
RT thread API reference manual
Variable star user module
Understanding of AMBA, AHB, APB and Axi
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
Whistle+switchyomega configure web proxy
1081 rational sum (20 points) points add up to total points
R & D thinking 01 ----- classic of embedded intelligent product development process
Imgcat usage experience
Use of lists