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

代码: css

为了探寻  ‘@Resource’, ‘@Autowired’, 和‘@Inject’如何解决依赖注入中的问题,我建立了一个“Party”接口,和它的两个实现类“Person”,“Organization”。这样我就能够在注入Bean的时候没必要使用具体类型(指使用接口类型便可)。这样作也方便我研究当一个接口有多个实现类与之匹配的时候Spring是如何解决依赖注入的不肯定性的。 html

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注入 java

这个测试验证注入Party的时候,当它有多个实现类的状况 spring

@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:字段类型注入 api

@Resource private Person party; @Autowired private Person party; @Inject private Person party;
    以上状况都将注入  ‘Person’ Bean。  

测试4:以实现类的默认名进行注入 ruby

@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:集合注入 ui

@Resource
private List<Party> parties;
@Autowired
private List<Party> parties
@Inject
private List<Party> parties;
      以上状况都将注入List中两个Bean。此方式一样能够用‘@Qualifier’限定注入Bean,每个知足指定‘qualifier’的bean才会被注入到List中。

测试7:不良配置 this

    用毫无关联的‘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

  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)
      ‘@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配置的可读性和稳定性。 

相关文章
相关标签/搜索