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配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分 ...
随机推荐
- vue2.0 + vux (三)MySettings 页
1.MySettings.vue <!-- 我的设置 --> <template> <div> <img class="img_1" sr ...
- mysql 执行sql文件的方法
http://philos.iteye.com/blog/162051 实战代码: #mysql导入mysql -um4n -p01D060A476642BA8335B832AC5B211F22 ...
- vim 查找整个工程
1. 使用vim内置搜索引擎 vimgrep 格式::vim /patern/gj ** 命令::vim 或者 :vimgrep 模式: 查询模式包含在 / / 之间 参数: g 表示将同一行搜到的关 ...
- [GUIDE] How to Setup Ubuntu 16.04 LTS Xenial Xerus for Compiling Android ROMs
With a new version of Ubuntu comes an update to my guide for setting up a build environment to compi ...
- Struts MVC工作原理(转载)
1.Struts MVC中Model 1 和Model 2简介 我们在开发Web应用时经常提到的一个概念是Model 1/Model 2,那么到底它是什么意思呢?其实它是对采用JSP技术构成Web应用 ...
- ios 使用自定义字体
本文转载至 http://blog.csdn.net/yesjava/article/details/8447596 1.下载要使用的自定义字体,格式通常为ttf.otf文件.这里假设是nokia ...
- csslint
http://csslint.net/ line column title description browserwarning 1 1 Disallow @import @import preven ...
- mount available
Mount (computing), the process of making a file system accessible mount (Unix), the utility in Unix- ...
- keybd_event、SendInput笔记
void keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, ULONG_PTR dwExtraInfo); bVk:虚拟键码 bScan:键的硬件扫描码 ...
- SpringBoot-(3)-RestController接口参数
一,无参接口: //无参接口 @RequestMapping("/appSecret") public String secret() { return "EK125EK ...