当前位置:网站首页>Difference between @Resource and @Autowired
Difference between @Resource and @Autowired
2022-08-11 05:18:00 【weixin_47088026】
The difference between @Resource and @Autowired
spring not only supports its own @Autowired annotations, but also supports several annotations defined by the JSR-250 specification,They are @Resource, @PostConstruct, and @PreDestroy.
@Resource
@Resource is equivalent to @Autowired, except that **@Autowired is automatically injected according to byType, while @Resource is automatically injected according to byName by default**.
@Resource has two important attributes, namely name and type. Spring parses the name attribute of the @Resource annotation as the name of the bean, and the type attribute parses it as the type of the bean.So if the name attribute is used, the automatic injection strategy of byName is used, and the automatic injection strategy of byType is used when the type attribute is used.If neither the name nor the type attribute is specified, the strategy will be automatically injected using byName through the reflection mechanism.
@Resource Assembly Order
- If both name and type are specified, the only matching bean will be found from the Spring context for assembly, and an exception will be thrown if not found
- If name is specified, look for a bean with a matching name (id) from the context for assembly, and throw an exception if it is not found
- If type is specified, find the only bean matching the type from the context for assembly, if not found or find more than one, an exception will be thrown
- If neither name nor type is specified, it will be assembled automatically according to the byName method; if there is no match, it will fall back to a primitive type for matching, and if it matches, it will be automatically assembled;
@Autowired
@Autowired is automatically injected according to byType. If a name is specified, it can be annotated with @Qualifier("cusInfoService"), generally @Autowired() and @Qualifier are generally used as decorations
The similarities and differences between @Resource and @Autowired
Resource is not in the spring framework, but Autowired is in spring
import javax.span>annotation.Resource;import org.springframework.beans.factory.annotation.span>Autowired;AutowiredAutowired injection only by type (if there is more than one bean of type UserDao in the Spring context, a BeanCreationException will be thrown; if there is no type of UserDao in the Spring contextbean, also throws BeanCreationException.), does not match the name for injection.Resource is more flexible. By default, it matches according to name (that is, the id of the bean in the example). Resource can specify whether to inject according to type or name by configuring its two properties: name and type.
@Autowired () @Qualifier ( "baseDao" )private BaseDao baseDao;@Resource (name= "baseDao" )private BaseDao baseDao;Recommended use: @Resource is annotated on the field, so there is no need to write a setter method, and this annotation belongs to J2EE, reducing the coupling with spring.This way the code looks more elegant.
@Autowired and @Resource can both be used to wire beans. Both can be written on fields, or on setter methods.
Summary:
- @Autowired//The default is to inject by type. When there are multiple beans of the same type, an error will be reported
- @Qualifier("cusInfoService") //Generally used as a modification of @Autowired()
- @Resource(name="cusInfoService") //Inject by name by default, you can selectively inject through name and type attributes
边栏推荐
- 02. Fold hidden text
- leetcode 9. Palindromic Numbers
- FPGA engineer interview questions collection 111~120
- paddlepaddle实现CS_CE Loss且并入PaddleClas
- 代理模式(简要介绍)
- IDEA中配置checkstyle
- [E-commerce operation] How to formulate a social media marketing strategy?
- Win10远程连接(实现多用户同时连接)
- Use Adobe genuine software for prostitution to reduce the slow employment and non-employment of fresh graduates
- 如何阅读论文
猜你喜欢

leetcode 9. Palindromic Numbers

玩转mysql之查看mysql版本号

How to switch Green Shield encryption to IP-Guard encryption smoothly

Idea essential skills to improve work efficiency

Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
![[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module](/img/7b/e265305df01eb405a131d0de2154d3.png)
[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module

Idea 2021.3.3版本文件目录展开

Switch and Router Technology - 32 - Named ACL

redis分布式锁

【电商运营】社交媒体营销策略该如何制定?
随机推荐
MySQL存储引擎概念
优化是一种习惯●出发点是“站在靠近临界“的地方
Day38 LeetCode
C statement: data storage
Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
Delphi7 learning record - demo example
2022 building welder (building a special type of work) examination questions and simulation test
async(异步)和await的使用
3 Module 2: Use of scientific research tools
3 模块二:科研工具使用
C语言题解:谁是凶手!
log4j2漏洞复现以及解决方案
HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
一个月闭关直接面进大厂,这份Android面试笔记是真的牛逼
[FPGA tutorial case 50] Control case 2 - FPGA-based PD controller verilog implementation
金仓数据库 KingbaseGIS 使用手册(6.10. 几何对象操作运算符)
2022年Android面试中最常问的问题是什么?
FPGA engineer interview questions collection 111~120
Unity WebGL RuntimeError: integer overflow(整数溢出问题)