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 ...
随机推荐
- ArchLinux 下 OpenSSH 高级运用
00x0.相关介绍 OpenSSH(OpenBSD Secure Shell)使用 SSH 通过计算机网络加密通信的实现. 它是替换由 SSH Communications Security 所提供的 ...
- 使用beautifulsoup和pyquery爬小说
# -*- coding:UTF-8 -*- from bs4 import BeautifulSoup #BeautifulSoup就是处理字符串的工具 import requests, sys & ...
- The server of Nginx(二)——Nginx基本功能配置
一.Nginx访问控制 (1)基于授权的访问控制 Nginx于Apache一样,可以实现基于用户授权的访问控制,当客户端要访问相应网站或者目录时要求输入用户名密码才能正常访问,配置步骤与Apache基 ...
- 使用记忆化优化你的 R 代码
目录 使用记忆化优化你的 R 代码 R 中的性能优化 R 何时变慢 R 何时变(更)快 R 中的记忆化 何时使用记忆化 使用记忆化优化你的 R 代码 本文翻译自<Optimize your R ...
- TP5.0学习历程(1)
验证码的使用 控制器代码如下: $captcha = new \think\captcha\Captcha(); if (!$captcha->check($data['code ...
- spider_keeper定时任务
# Define apscheduler app 下的 __init__.py 文件中, 三个主要函数
- centos的基本命令01
01:创建用户命令 useradd liulaoshi # 创建用户 passwd liulaoshi # 设置密码, 回车后会提示让你输入两次密码 1.用户列表文件:/etc/passwd/ 2.用 ...
- SQL的CharIndex用法
和C#一样判断一个字符串中是否包含另一个字符串举例1:select charindex('test','This Test is test!!')->返回 6 (空格也算一个 下标从1开始)2: ...
- dp--hdu1171(01背包)
hdu1171 题目 Problem Description Nowadays, we all know that Computer College is the biggest department ...
- linux操作python
Linux安装python3: sudo apt-get install python3.5 安装python库 sudo apt-get install python3-setuptools sud ...