一些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. LINUX下目标文件的BSS段、数据段、代码段

    http://blog.chinaunix.net/uid-27018250-id-3867588.html bss 未初始化的全局数据 data 已经初始化的全局数据 text 代码段,机器指令 r ...

  2. hihocoder 1032 manachar 求回文串O(n)

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  3. wdcp新开站点或绑定域名打不开或无法访问的问题

    一 用IP可以打开,但用域名打开网站显示到默认页面1  站点列表里是否有相应的网站信息 2  检查有没站点配置文件后台 >系统管理 >文件管理器 >虚拟主机站点文件(nginx,ap ...

  4. 三行代码实现.NET MVC统计显示页面的执行时间 超简单的实现方法 分析页面执行效率

    三行代码实现.NET MVC统计显示页面的执行时间 超简单的实现方法 分析页面执行效率    博客页脚处添加了页面执行时间统计显示,如下图所示,也可以直接查看网页页脚处. 实现方法非常简单,只需三行代 ...

  5. Mysql导出大量数据

    outfile 导出文件   select name from t1 into outfile "/tmp/test.txt"   infile 导入文件 导入到表t1中的name ...

  6. redirect_uri 參数错误的解决的方法

    我通过java代码去获得用户的openid,一直报redirect_uri. 我页面代码的链接为: https://open.weixin.qq.com/connect/oauth2/authoriz ...

  7. DB 【ACID】

    http://blog.csdn.net/shuaihj/article/details/14163713 http://blog.csdn.net/dief913975849/article/det ...

  8. Python的专有属性

  9. 关于erlang反编译的东西

    在查阅了相关文档,想了解erlang反编译的东西.当然,源码可以打包成可以获取源码的,也可以保护源码的. 在ebin下,如果没有或者找不到源码,可以进行反编译,由beam文件得到erl文件. 可以通过 ...

  10. EasyPusher RTSP直播之RTP数据包格式解析

    -本篇由团队成员Fantasy供稿! RTP包头格式 码流总体结构 h264的功能分为两层,视频编码层(VCL)和网络提取层(NAL).H.264 的编码视频序列包括一系列的NAL 单元,每个NAL ...