您的位置:首页 > 编程语言 > Java开发

spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异

2016-10-30 17:19 746 查看
为了探寻  ‘@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配置的可读性和稳定性。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐