Spring 注释标签@Resource @Autowired 和@Inject的区别
一些spring的开发人员在使用这三个标签进行注入的时候感到困惑。我来尝试解释一下这三个注解的主要区别。事实上,这三者非常相似,只存在一些微小的差别。在稍后的文章中会进行解释。
- @Resource-在javax.annotation包中定义
- @Inject-在javax.inject包中定义
- @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的区别的更多相关文章
- 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 ...
- spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异
为了探寻 '@Resource', '@Autowired', 和'@Inject'如何解决依赖注入中的问题,我创建了一个"Party"接口,和它的两个实现类"Perso ...
- Spring @Resource, @Autowired and @Inject 注入
Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...
- Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理
一.spring依赖注入使用方式 @Autowired是spring框架提供的实现依赖注入的注解,主要支持在set方法,field,构造函数中完成bean注入,注入方式为通过类型查找bean,即byT ...
- Spring 注释 @Autowired 和@Resource
一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired属于Spring的:@Resource为JSR-250标准的注释,属于J ...
- Spring 注释 @Autowired 和@Resource 的区别
Spring 注释 @Autowired 和@Resource 的区别 一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired ...
- annotation之@Autowired、@Inject、@Resource三者区别
一.@Autowired 1.@Autowired是spring自带的注解,通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入: 2.@Autowire ...
- SpringBoot入门教程(十六)@Autowired、@Inject、@Resource
@Resource,@Autowired,@Inject 这3种都是用来注入bean的,它们属于不同的程序中.详情参见下表: v区别 ANNOTATION PACKAGE SOURCE 作用域 实现方 ...
- 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置
spring-import 标签的解析.使用案例: 对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分 ...
随机推荐
- CentOS6.5_64bit下编译安装MySQL-5.6.23
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/44785511 ************************************** ...
- 向odoo贡献中文翻译
建议通过 osc-git向odoo贡献中文翻译 osc-git 是指'开源中国'的git平台. 网址是 http://git.oschina.net/ 注册osc-git 账号省略. ...
- python(7)- 小程序练习:循环语句for,while实现99乘法表
打印99乘法表 for 循环语句实现: for i in range(1,10): for j in range(1,10): print(j,"x",i,"=" ...
- JavaScript闭包其一:闭包概论 函数式编程中一些基本定义
http://www.nowamagic.net/librarys/veda/detail/1707前面介绍了作用域链和变量对象,现在再讲闭包就容易理解了.闭包其实大家都已经谈烂了.尽管如此,这里还是 ...
- UVA11770 - Lighting Away
题目链接 题意:一个有向图,每对一个结点操作.就能够触发连锁反应,使得该结点及它直接或间接指向的点均获得标记,问至少须要操作多少个结点使得全部结点获得标记 思路:有向图的强连通分量.用Tarjan缩点 ...
- 增加录像时间戳水印、 camera框架介绍
http://blog.csdn.net/mirkerson/article/details/38920107 http://blog.csdn.net/jimbo_lee/article/detai ...
- Android.mk: recipe commences before first target. Stop.
[GUIDE] Setup Android Development Environment on Ubuntu 14.04 Trusty Tahr Hi All, This originally wa ...
- Linux随笔记
Linux配置apt-get源地址 以Ubuntu配置网易开源镜像站为例: 访问地址:http://mirrors.163.com/,找到对应的系统. 先将source.list进行备份,执行: su ...
- etymology-I
1)inter-.intra-.intro- 三个前缀inter-,intra-和intro-还是有差别的. inter-表between,如international那是between differ ...
- EasyVideoRecorder短视频拍摄、短视频录制SDK支持IOS版本
在前面的博客<EasyDarwin开发出类似于美拍.秒拍的短视频拍摄SDK:EasyVideoRecorder>和<美拍.秒拍中安卓.IOS短视频拍摄的一些关键技术>中我们简单 ...