温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

@Resources, @Inject和@Autowired的区别是什么

发布时间:2021-11-03 10:32:50 来源:亿速云 阅读:141 作者:iii 栏目:编程语言

这篇文章主要介绍“@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

注入的优先级:

  1. Match by Type

  2. Match by Qualifier

  3. 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的区别是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI