当前位置:网站首页>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
边栏推荐
- Win10远程连接(实现多用户同时连接)
- MFC 进程间通信(共享内存)
- 一个月闭关直接面进大厂,这份Android面试笔记是真的牛逼
- 用白嫖的Adobe正版软件,减少应届毕业生的慢就业、不就业等现象
- [Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
- Prometheus :(一)基本概念
- [ARM] rk3399 mounts nfs error
- In the closing pages/uninstall (unload) sends a request to the server before the document
- 批量修改数据库等视频文件名称
- Let's talk programming languages together
猜你喜欢
redis连接idea
Configure checkstyle in IDEA
Switch and Router Technology - 36-Port Mirroring
[ARM] rk3399 mounts nfs error
HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
什么是三次握手和四次挥手(清晰易懂)
Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
C statement: data storage
Idea 2021.3.3版本文件目录展开
Switch and Router Technology-34-Dynamic NAT
随机推荐
How to use svg-icon (svg-sprite-loader plugin)
leetcode 9. Palindromic Numbers
You must understand - the nine built-in objects and four domain objects of JSP
论文笔记:BBN: Bilateral-Branch Network with Cumulative Learningfor Long-Tailed Visual Recognition
2021 Network Planning Designer Afternoon Case Questions
Redis-数据类型(基本指令、String、List、Set、Hash、ZSet、BitMaps、HyperLogLog、GeoSpatial)/发布和订阅
2022 building welder (building a special type of work) examination questions and simulation test
Delphi7 learning record - demo example
Idea essential skills to improve work efficiency
IDEA中配置checkstyle
【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
【嵌入式开源库】MultiTimer 的使用,一款可无限扩展的软件定时器
Use Adobe genuine software for prostitution to reduce the slow employment and non-employment of fresh graduates
BitLocker的解密
Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
Switches and routers technology - 24 - configure OSPF single area
Win10远程连接(实现多用户同时连接)
【电商运营】社交媒体营销策略该如何制定?
分库分表ShardingSphere-JDBC笔记整理
Switches and routers technology - 21 - RIP routing protocol