当前位置:网站首页>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
边栏推荐
- Unity WebGL RuntimeError: integer overflow(整数溢出问题)
- Win10远程连接(实现多用户同时连接)
- 02.折叠隐藏文字
- Switches and routers technology - 21 - RIP routing protocol
- C language antithesis: who is the murderer!
- 提升你工作效率的技巧,你得知道——Navitcat 快捷键
- MySQL must know and must know (primary articles)
- 优化是一种习惯●出发点是“站在靠近临界“的地方
- Application layer protocol - DNS
- Core Data 多线程设计
猜你喜欢
随机推荐
About CC Attacks
2022年Android面试中最常问的问题是什么?
zabbix构建企业级监控告警平台
四大函数式接口
paddlepaddle implements CS_CE Loss and incorporates PaddleClas
redis分布式锁
Configure checkstyle in IDEA
K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster
C语言题解:谁是凶手!
C statement: data storage
批量修改数据库等视频文件名称
Switch and Router Technology-27-OSPF Route Redistribution
C language antithesis: who is the murderer!
2022 coal mine gas inspection test, simulation test question and answer
Switches and routers technology - 24 - configure OSPF single area
元宇宙社交应用,靠什么吸引用户「为爱发电」?
什么是三次握手和四次挥手(清晰易懂)
Delphi7学习记录-demo实例
2021 Network Planning Designer Afternoon Case Questions
Core Data 多线程设计








