一些spring的开发人员在使用这三个标签进行注入的时候感到困惑。我来尝试解释一下这三个注解的主要区别。事实上,这三者非常相似,只存在一些微小的差别。在稍后的文章中会进行解释。

  1. @Resource-在javax.annotation包中定义
  2. @Inject-在javax.inject包中定义
  3. @Autowired-在org.springframework.bean.factory包中定义

我们创建一个Car接口和两个实现类Volkswagen和Toyota.分别通过三种标签来注入来观察差异. 接口和类的定义如下.这里只提供了代码片段,如果你想运行这个例子,需要新建一个spring项目。

示例:

package com.dxz.beanregs;
public interface Car {
}
package com.dxz.beanregs; import org.springframework.stereotype.Component; @Component
public class Toyota implements Car {
} package com.dxz.beanregs; import org.springframework.stereotype.Component; @Component
public class Volkswagen implements Car {
}

Inject Interface

@Resource
private Car car; @Autowired
private Car car; @Inject
private Car car;

下面是抛出的异常:
Resource注解抛出:org.springframework.beans.factory.NoSuchBeanDefinitionException:
Autowired注解抛出:No unique bean of type [javabeat.net.basics.Car] is defined:
Inject注解抛出:expected single matching bean but found 2: [volkswagen, toyota]

Field Type

@Resource
private Volkswagen car; @Autowired
private Volkswagen car; @Inject
private Volkswagen car;

上面的代码工作的很好。 通过bean type,三个注解都注入了Volkswagen.

Qualifier name

@Resource
@Qualifier("volkswagen")
private Car car; @Autowired
@Qualifier("volkswagen")
private Car car; @Inject
@Qualifier("volkswagen")
private Car car;

上面三个注解结合了@Qualifier将Volkswagen成功注入了。

Conflicting Information

@Resource
@Qualifier("nkl")
private Car volkswagen; @Autowired
@Qualifier("nkl")
private Car volkswagen; @Inject
@Qualifier("nkl")
private Car volkswagen;

上面的代码,只有@Resource注入了Volkswagen类型.但是,@Autowired和@Injects都抛出了异常.
Resource注解抛出:org.springframework.beans.factory.NoSuchBeanDefinitionException:
Autowired注解抛出:No matching bean of type [javabeat.net.basics.Car] found for dependency:

所以主要的区别是:@Autowired和@Inject无区别,这两个注解都是通过AutowiredAnnotationBeanPostProcessor来注入依赖。但是@Resource使用CommonAnnotationBeanPostProcessor来注入依赖。主要的区别是在检查的顺序上。

    • @Autowired and @Inject

      • 1.Matches by Type
      • 2.Restricts by Qualifiers
      • 3.Matches by Name
    • @Resource

      • 1.Matches by Name
      • 2.Matches by Type
      • 3.Restricts by Qualifiers (ignored if match is found by name)

Spring 注释标签@Resource @Autowired 和@Inject的区别的更多相关文章

  1. Spring Injection with @Resource, @Autowired and @Inject

    August 1st, 2011 by David Kessler Overview I’ve been asked several times to explain the difference b ...

  2. spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异

    为了探寻 '@Resource', '@Autowired', 和'@Inject'如何解决依赖注入中的问题,我创建了一个"Party"接口,和它的两个实现类"Perso ...

  3. Spring @Resource, @Autowired and @Inject 注入

    Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...

  4. Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理

    一.spring依赖注入使用方式 @Autowired是spring框架提供的实现依赖注入的注解,主要支持在set方法,field,构造函数中完成bean注入,注入方式为通过类型查找bean,即byT ...

  5. Spring 注释 @Autowired 和@Resource

    一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired属于Spring的:@Resource为JSR-250标准的注释,属于J ...

  6. Spring 注释 @Autowired 和@Resource 的区别

    Spring 注释 @Autowired 和@Resource 的区别 一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired ...

  7. annotation之@Autowired、@Inject、@Resource三者区别

    一.@Autowired 1.@Autowired是spring自带的注解,通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入: 2.@Autowire ...

  8. SpringBoot入门教程(十六)@Autowired、@Inject、@Resource

    @Resource,@Autowired,@Inject 这3种都是用来注入bean的,它们属于不同的程序中.详情参见下表: v区别 ANNOTATION PACKAGE SOURCE 作用域 实现方 ...

  9. 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置

    spring-import 标签的解析.使用案例: 对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分 ...

随机推荐

  1. 设置安卓开机动画、开机logo

    我们要修改的是system>media文件夹下的bootanimation.zip(手机开机动画)这个文件 先来讲讲这个文件结构:该zip解压后得到两个文件, 第一个目录存放了开机时播放的图片( ...

  2. 在CentOS 7上构建RAID5、LVM和SAMBAserver(5)——架设SAMBAserver

    在CentOS 7上构建RAID5.LVM和SAMBAserver(5)--架设SAMBAserver 6. 架设SAMBAserver 6.1. 预备 本节的任务是配置SAMBA服务,共享/home ...

  3. 自己动手写CPU之第七阶段(5)——流水线暂停机制的设计与实现

    将陆续上传本人写的新书<自己动手写CPU>,今天是第28篇.我尽量每周四篇 China-pub的预售地址例如以下(有文件夹.内容简单介绍.前言): http://product.china ...

  4. C#Soket组播

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  5. BZOJ 2818 Gcd 线性欧拉

    题意:链接 方法:线性欧拉 解析: 首先列一下表达式 gcd(x,y)=z(z是素数而且x,y<=n). 然后我们能够得到什么呢? gcd(x/z,y/z)=1; 最好还是令y>=x 则能 ...

  6. 多媒体开发之---h.264 SPS PPS解析源代码,C实现一以及nal分析器

    http://blog.csdn.net/mantis_1984/article/details/9465909 http://blog.csdn.net/arau_sh/article/detail ...

  7. html中设置透明遮罩层的兼容性代码

    说明:下面遮罩层的height视实际情况自行修改,要求显示的div层的样式需加上position:relative,位于遮罩层层div的下面一行.<div id="ceng" ...

  8. EasyPusher/EasyDarwin/EasyPlayer实现手机直播版本及效果整理

    EasyPusher手机直播 实现功能 最近很多EasyDarwin爱好者提出了手机移动端直播的功能需求,尤其是如何做出像映客这样能够快速出画面播放的效果,经过一段时间的移动端和服务端的优化,Easy ...

  9. GO 入门(一)

    1.下载安装go环境          https://golang.org/dl/ 2.检查环境变量配置情况,安装过程中会自动配置:GOROOT    和    Path 3.建立go工作区,并配置 ...

  10. 什么是 CAS 机制?

    1.Synchronized关键字 Synchronized关键字会让没有得到锁资源的线程进入BLOCKED状态,而后在争夺到锁资源后恢复为RUNNABLE状态,这个过程中涉及到操作系统用户模式和内核 ...