当前位置:网站首页>@The difference between Autowired and @resource
@The difference between Autowired and @resource
2022-07-06 12:17:00 【A pole】
@Autowired and @Resource The difference between
Different default injection methods
@Autowired The default injection method is byType( Match by type ), In other words, it will give priority to matching and injecting according to the interface type Bean ( Implementation class of interface ), If you want to specify a name , Can pass @Qualifier In combination with .
@Autowired
private UserSafetyMapper userSafetyMapper;
// or
@Autowired @Qualifier("userSafetyMapper")
private UserSafetyMapper userSafetyMapper;
@Resource The default injection method is byName( Match according to the name ). If you cannot match the corresponding implementation class by name , The injection method will change to byType.
- If you also specify name and type, From Spring Unique match found in context bean Assembly , Throw exception if not found ;
- If you specify name, Find name from context (id) Matching bean Assembly , Throw exception if not found ;
- If you specify type, Then find the unique type match from the context bean Assembly , Can't find or find more than one , Will throw an exception ;
- If neither name, It's not specified type, Then automatically follow the byName Method of assembly ; If there is no match , Back off to match as an original type , Auto assemble if match .
// 1. Default injection mode
@Resource
private UserSafetyMapper userSafetyMapper;
// 2. Specify the injection method
@Resource(name = "userSafetyMapper", type = UserSafetyMapper.class)
private UserSafetyMapper userSafetyMapper;
Different providers
@Autowired yes spring Notes provided ,@Resource yes JDK Notes provided
边栏推荐
猜你喜欢
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
E-commerce data analysis -- salary prediction (linear regression)
Basic knowledge of lithium battery
The dolphin scheduler remotely executes shell scripts through the expect command
History object
AMBA、AHB、APB、AXI的理解
Redis based distributed locks and ultra detailed improvement ideas
CUDA C programming authoritative guide Grossman Chapter 4 global memory
C language callback function [C language]
Basic operations of databases and tables ----- classification of data
随机推荐
Common properties of location
OSPF message details - LSA overview
Page performance optimization of video scene
Arduino get random number
Priority inversion and deadlock
There are three iPhone se 2022 models in the Eurasian Economic Commission database
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Générateur d'identification distribué basé sur redis
JS正则表达式基础知识学习
Cannot change version of project facet Dynamic Web Module to 2.3.
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Knowledge summary of request
列表的使用
js 变量作用域和函数的学习笔记
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
History object
Pytorch: tensor operation (I) contiguous
Redis based distributed ID generator
Cannot change version of project facet Dynamic Web Module to 2.3.
Kconfig Kbuild