spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异
为了探寻 ‘@Resource’, ‘@Autowired’, 和‘@Inject’如何解决依赖注入中的问题,我创建了一个“Party”接口,和它的两个实现类“Person”,“Organization”。这样我就可以在注入Bean的时候不必使用具体类型(指使用接口类型即可)。这样做也方便我研究当一个接口有多个实现类与之匹配的时候Spring是如何解决依赖注入的不确定性的。
public interface Party {}
package com.sourceallies.person;
...
@Component
public class Person implements Party {}
package com.sourceallies.organization;
...
@Component
public class Organization implements Party {}
在Spring的配置文件中设置使用 ‘@Component’注解的两个实现类所在的包需要进行注入检查
<context:component-scan base-package="com.sourceallies.organization"/>
<context:component-scan base-package="com.sourceallies.person"/>
测试1:不明确的bean注入
这个测试验证注入Party的时候,当它有多个实现类的情况
@Resource
private Party party;
@Autowired
private Party party;
@Inject
private Party party;
以上三种情况抛出同样的 ‘NoSuchBeanDefinitionException’异常,单看异常名称意味着不存在对应的Bean,不过详细信息中显示找到了两个Bean。
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [com.sourceallies.Party] is defined:
expected single matching bean but found 2: [organization, person]
测试2:字段名称注入
@Resource
private Party person;
@Autowired
private Party person;
@Inject
private Party person;
其中@Resource注入可以设置可选项‘ name’属性,以下语法和上面的@Resource注解方式是等效的,但是@Autowired和@Inject就没有类似的等价方式了。
@Resource(name="person")
private Party party;
以上三种方式最终都被注入‘Person’ Bean。
测试3:字段类型注入
@Resource
private Person party;
@Autowired
private Person party;
@Inject
private Person party;
以上情况都将注入 ‘Person’ Bean。
测试4:以实现类的默认名进行注入
@Resource
@Qualifier("person")
private Party party;
@Autowired
@Qualifier("person")
private Party party;
@Inject
@Qualifier("person")
private Party party;
以上情况都将注入 ‘Person’ Bean。
测试5:指定实现类的类名
在实现类中使用‘Qualifier’注解指定注入时使用的名称
package com.sourceallies.person;
...
@Component
@Qualifier("personBean")
public class Person implements Party {}
注入的时候同样使用‘Qualifier’注解指定注入哪一个名称的实现类
@Resource
@Qualifier("personBean")
private Party party;
@Autowired
@Qualifier("personBean")
private Party party;
@Inject
@Qualifier("personBean")
private Party party;
以上情况都将注入 ‘Person’ Bean。
测试6:集合注入
@Resource
private List<Party> parties;
@Autowired
private List<Party> parties
@Inject
private List<Party> parties;
以上情况都将注入List中两个Bean。此方式同样可以用‘@Qualifier’限定注入Bean,每一个满足指定‘qualifier’的bean才会被注入到List中。
测试7:不良配置
用毫无关联的‘bad’作为‘@Qualifier’指定的匹配名
@Resource
@Qualifier("bad")
private Party person;
@Autowired
@Qualifier("bad")
private Party person;
@Inject
@Qualifier("bad")
private Party person;
这种情况下使用‘@Resource’注解将会忽略‘@Qualifier’配置,故而‘Person' Bean将被注入。
而后两者将会抛出 ‘NoSuchBeanDefinitionException’ 的错误信息,因为找不到与’@Qualifier‘配置相匹配的bean。
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.sourceallies.Party] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true),
@org.springframework.beans.factory.annotation.Qualifier(value=bad)}
总结
‘@Autowired’ 和‘@Inject’的报错信息完全相同,他们都是通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入,二者具有可互换性。
‘@Resource’通过 ‘CommonAnnotationBeanPostProcessor’ 类实现依赖注入,即便如此他们在依赖注入时的表现还是极为相近的,以下是他们在实现依赖注入时执行顺序的概括:
@Autowired and @Inject
- Matches by Type
- Restricts by Qualifiers
- Matches by Name
@Resource
- Matches by Name
- Matches by Type
- Restricts by Qualifiers (ignored if match is found by name)
‘@Resource’在依据name注入的时候速度性能表现的比 ‘@Autowired’ 和‘@Inject’优越,但这是微不足道的,不足以作为优先选择 ‘@Resource’的原因。我倾向于使用 ‘@Resource’是因为它配置起来更简洁。
@Resource(name="person")
@Autowired
@Qualifier("person")
@Inject
@Qualifier("person")
你也许会说使用字段 默认 名称作为注入时候的bean name,其他两种方式就会一样简洁:
@Resource
private Party person;
@Autowired
private Party person;
@Inject
private Party person;
确实如此。但是当你需要重构代码的时候又如何呢?使用’@Resource‘方式只需简单修改name属性即可,而无需触及注入Bean的名称(注入Bean的时候同意使用接口名称)。所以我建议使用注解方式实现注入的时候遵循以下语法风格:
1.在你的组件中明确限定bean名称而不是使用默认值[@Component("beanName")]。
2.同时使用’@Resource‘和它的’name'属性 [@Resource(name="beanName")]。
3.避免使用‘@Qualifier’注解,除非你要创建一系列类似beans的集合。例如,你也许需要建立一个set集合来存放一系列“规则”定义。这个时候可以选择‘@Qualifier'注解方式。这种方式使得将大量遵循相同规则的类放入集合中变得容易。
4.使用如下配置限定需要尽心组件扫描的包: [context:component-scan base-package="com.sourceallies.person"]。这样做可以减小spring扫描很多无效的包的情况。
遵循以上原则能增强你的,注解风格的,spring配置的可读性和稳定性。
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的区别
一些spring的开发人员在使用这三个标签进行注入的时候感到困惑.我来尝试解释一下这三个注解的主要区别.事实上,这三者非常相似,只存在一些微小的差别.在稍后的文章中会进行解释. @Resource-在 ...
- Spring @Resource, @Autowired and @Inject 注入
Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...
- 07 Spring框架 依赖注入(四)基于注解的依赖注入
前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...
- Spring-Context的注解实现依赖注入功能
使用Spring-Context的注解实现依赖注入功能. Demo要点: 本例子中主要使用Annotation功能来实现对MoviceService的注入.我们将Cinema.java的头部标注为@C ...
- 【Spring Framework】Spring 入门教程(一)控制反转和依赖注入
参考资料 Spring 教程 说在前面 什么样的架构,我们认为是一个优秀的架构? 判断准则:可维护性好,可扩展性好,性能. 什么叫可扩展性好? 答:在不断添加新的代码的同时,可以不修改原有代码,即符合 ...
- Spring @Resource,@Autowired,@Qualifier的注解注入和区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- spring通过注解方式依赖注入原理 (私有成员属性如何注入)
一.spring如何创建依赖的对象 用过spring的都知道我们在dao.service层加上@repository.@Service就能将这两个对象交给spring管理,在下次使用的时候使用@res ...
- Spring框架第四篇之基于注解的DI注入
一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...
随机推荐
- docker导入导出镜像
docker容器导入导出有两种方法: 一种是使用save和load命令 使用例子如下: docker save ubuntu:load>/root/ubuntu.tar docker load& ...
- Flume启动运行时报错org.apache.flume.ChannelFullException: Space for commit to queue couldn't be acquired. Sinks are likely not keeping up with sources, or the buffer size is too tight解决办法(图文详解)
前期博客 Flume自定义拦截器(Interceptors)或自带拦截器时的一些经验技巧总结(图文详解) 问题详情 启动agent服务 [hadoop@master flume-1.7.0]$ ...
- 实验吧之deeeeeeaaaaaadbeeeeeeeeeef-200
题目中提示说“图片是正确的吗”,赶紧打开图片,图片显示正常,没啥毛病,那就放到winhex里面,好像它的十六进制格式也蛮标准的,然后它的文本区域有个iphone,这个梗我也是百度才知道的: winhe ...
- SAP生产订单各种日期的计算说明
生产订单各种日期的计算说明 基本日期.已计划的.确认的日期,介绍一下这些日期的作用和计算方法: 首先我们来介绍一下基本日期: 基本开始日期:表示订单的开始日期 基本完成日期:表示订单的完成日期 我们在 ...
- SQL数据库Replace的用法
关于数据库Replace的用法:Replace("字符串","要被替代的字符串","替代后的字符串")尝试过写法效果如下->修改前 效 ...
- python 反射和内置方法
一.isinstance和issubclass class Foo: pass class Son(Foo): pass s = Son() #判断一个对象是不是这个类的对象,传两个参数(对象,类) ...
- 启用mysql日志记录执行过的sql
在mysql命令行或者客户端管理工具中执行:SHOW VARIABLES LIKE "general_log%"; 结果: general_log OFF general_log_ ...
- DataGridView绑定list的注意事项
1.DataGridView数据绑定对比(DataTable与泛型List): 当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时, ...
- springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux
//我的会员中心 头像上传接口 /*windows 调试*/ @Value("${appImg.location}") private String winPathPic; /*l ...
- 在vue中import()语法不能传入变量
解决办法: 一定要用变量的时候,可以通过字符串模板来提供部分信息给webpack:例如import(`./path/${myFile}`), 这样编译时会编译所有./path下的模块,但运行时确定my ...