@Resources, @Inject和@Autowired的区别是什么
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍"@Resources, @Inject和@Autowired的区别是什么",在日常操作中,相信很多人在@Resources, @Inject和@Autowired的区别是什么问题上存
千家信息网最后更新 2025年11月08日@Resources, @Inject和@Autowired的区别是什么
这篇文章主要介绍"@Resources, @Inject和@Autowired的区别是什么",在日常操作中,相信很多人在@Resources, @Inject和@Autowired的区别是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"@Resources, @Inject和@Autowired的区别是什么"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
@Resources
官方文档里对@Resources的说明:
The @Resource annotation is part of the JSR-250 annotation collection and is packaged with Jakarta EE.
什么是JSR-250呢?访问这个链接:https://id=250https://id=330
注入的优先级:
Match by Type
Match by Qualifier
Match by Name
Match by Type
注意@Inject注入的最高优先级方式为Match by Type,而非@Resource的Match by Name.
任意定义一个待注入的Component:
@Componentpublic class ArbitraryDependency { private final String label = "Arbitrary Dependency"; public String toString() { return label; }}使用@Inject注入:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration( loader=AnnotationConfigContextLoader.class, classes=ApplicationContextTestInjectType.class)public class FieldInjectIntegrationTest { @Inject private ArbitraryDependency fieldInjectDependency; @Test public void givenInjectAnnotation_WhenOnField_ThenValidDependency(){ assertNotNull(fieldInjectDependency); assertEquals("Arbitrary Dependency", fieldInjectDependency.toString()); }}Match by Qualifier
定义一个新的待注入组件:
public class AnotherArbitraryDependency extends ArbitraryDependency { private final String label = "Another Arbitrary Dependency"; public String toString() { return label; }}测试代码:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration( loader=AnnotationConfigContextLoader.class, classes=ApplicationContextTestInjectQualifier.class)public class FieldQualifierInjectIntegrationTest { @Inject private ArbitraryDependency defaultDependency; @Inject private ArbitraryDependency namedDependency; @Test public void givenInjectQualifier_WhenOnField_ThenDefaultFileValid(){ assertNotNull(defaultDependency); assertEquals("Arbitrary Dependency", defaultDependency.toString()); } @Test public void givenInjectQualifier_WhenOnField_ThenNamedFileValid(){ assertNotNull(defaultDependency); assertEquals("Another Arbitrary Dependency", namedDependency.toString()); }}和之前@Resource的第一次试图通过Match by Type注入一样失败,遇到异常:NoUniqueBeanDefinitionException
利用@Qualifier避免这个异常:
@Inject@Qualifier("defaultFile")private ArbitraryDependency defaultDependency;@Inject@Qualifier("namedFile")private ArbitraryDependency namedDependency;Match by Name
public class YetAnotherArbitraryDependency extends ArbitraryDependency { private final String label = "Yet Another Arbitrary Dependency"; public String toString() { return label; }}消费者代码:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration( loader=AnnotationConfigContextLoader.class, classes=ApplicationContextTestInjectName.class)public class FieldByNameInjectIntegrationTest { @Inject @Named("yetAnotherFieldInjectDependency") private ArbitraryDependency yetAnotherFieldInjectDependency; @Test public void givenInjectQualifier_WhenSetOnField_ThenDependencyValid(){ assertNotNull(yetAnotherFieldInjectDependency); assertEquals("Yet Another Arbitrary Dependency", yetAnotherFieldInjectDependency.toString()); }}application context代码:
@Configurationpublic class ApplicationContextTestInjectName { @Bean public ArbitraryDependency yetAnotherFieldInjectDependency() { ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency(); return yetAnotherFieldInjectDependency; }}到此,关于"@Resources, @Inject和@Autowired的区别是什么"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
学习
代码
优先级
更多
帮助
实用
最高
接下来
官方
文档
文章
方式
方法
消费者
理论
知识
第一次
篇文章
组件
网站
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
珠海录播服务器价格
网络安全和信息化工作月报
信息化与网络安全工作会议
图片批量上传到服务器
天津数据库培训条件
云服务器的远程桌面连接黑屏
数据库迁移的技术
德国华为网络安全
orcal数据库分解
中国灾害数据库
软件开发需要什么工具
数据库文档设计工具
原神服务器怎么区别
上海精英网络技术咨询商家
错误删除plsql数据库
快递物流管理软件开发
搭建vpn服务器要多少钱
海光服务器cpu上市公司
山东dns服务器托管云主机
配置连接远端数据库
网络技术好还是数字媒体好
爱思如何查看手机数据库
北京服务器硬盘回收
2017中国网络安全大事
创造与魔法新服务器294
网络安全检查的主要项目
柯美数字印刷机 服务器
临沂直销软件开发
中兴 笔试 软件开发
爬虫软件开发外包违法