《Spring Recipes》第一章筆記:@Autowired and @Resource

@Autowired and @Resource


問題:

在bean標籤上使用autowire屬性,會將bean的全部屬性都設置爲autowire。沒法作到指定特定的屬性爲autowire。

解決方案

從Spring2.5開始,能夠在方法、構造函數、屬性上添加@Autowired或者@Resource註解進行注入。

配置AutowiredAnnotationBeanPostProcessor

想要使用@Autowired和@Resource註解,必須在容器中註冊一個AutowiredAnnotationBeanPostProcessor實例。

配置方法:
能夠直接在配置文件中聲明一個實例:
<bean class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor" />

或者在配置文件中配置<context:annotation-config>:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
...
</beans>

配置<context:annotation-config>後,容器會自動註冊AutowiredAnnotationBeanPostProcessor。

@Autowired註解

使用@Autowired注入單個合適類型的bean

bean:
public class SequenceGenerator {
... ...
    @Autowired
    public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
        this.prefixGenerator = prefixGenerator;
    }
}

配置文件:
<beans ...>
... ...
  <bean id="sequenceGenerator"
    class="com.apress.springrecipes.sequence.SequenceGenerator">
    <property name="initial" value="100000" />
    <property name="suffix" value="A" />
  </bean>
  
  <bean id="datePrefixGenerator"
    class="com.apress.springrecipes.sequence.DatePrefixGenerator">
    <property name="pattern" value="yyyyMMdd" />
  </bean>

</beans>

容器會自動尋找PrefixGenerator類型的bean,並注入到 setPrefixGenerator函數中。

使用@Autowired注入多個個合適類型的bean

使用@Autowired能夠像數組和集合類型中注入多個合適類型的bean。
容器會將配置文件中全部適合類型的bean注入到數組或者集合中。
bean:
public class SequenceGenerator {
    @Autowired
    private PrefixGenerator[] prefixGenerators;
    ... ...
}

配置文件:
<beans ...>
...
  <bean id="datePrefixGenerator" class="com.apress.springrecipes.sequence.DatePrefixGenerator">
    <property name="pattern" value="yyyyMMdd" />
  </bean>

  <bean id="yearPrefixGenerator"
  class="com.apress.springrecipes.sequence.DatePrefixGenerator">
    <property name="pattern" value="yyyy" />
  </bean>

</beans>

List:
public class SequenceGenerator {
    @Autowired
    private List<PrefixGenerator> prefixGenerators;
... ...
}

Map:
public class SequenceGenerator {
    @Autowired
    private Map<String, PrefixGenerator> prefixGenerators;
... ...
}


使用@Qualifier註解

默認狀況下,容器不能處理配置文件中有相同類型bean的注入。能夠經過@Qualifier註解來解決這個問題。
bean:
public class SequenceGenerator {
    @Autowired
    @Qualifier("datePrefixGenerator")
    private PrefixGenerator prefixGenerator;
... ...
}

配置文件:
<bean class="com.apress.springrecipes.sequence.DatePrefixGenerator">
  <qualifier value="datePrefixGenerator"/>
  <property name="pattern" value="yyyyMMdd" />
</bean>

能夠在函數的參數上使用@Qualifier註解:java

public class SequenceGenerator {
...
    @Autowired
    public void inject(
      @Qualifier("datePrefixGenerator") PrefixGenerator prefixGenerator) {
      this.prefixGenerator = prefixGenerator;
    }
}

使用自定義的Qualifier

qualifier:
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Generator{
  String value();
}

bean:
public class SequenceGenerator {
	@Autowired
	@Generator("prefix")
	private PrefixGenerator prefixGenerator;
	... ...
}

配置文件:
<bean id="datePrefixGenerator"
class="com.apress.springrecipes.sequence.DatePrefixGenerator">
  <qualifier type="Generator" value="prefix" />
  <property name="pattern" value="yyyyMMdd" />
</bean>



@Resource註解

能夠經過JSR-250 @Resource註解,實現經過名稱進行注入。@Resource註解能夠添加在setter方法、構造函數或者屬性上。
默認狀況下,@Resource註解查找和添加了註解的屬性同名的bean進行注入。用戶能夠在@Resource註解中指定要注入的bean的名稱。
public class SequenceGenerator {
    @Resource(name = "datePrefixGenerator")
    private PrefixGenerator prefixGenerator;
... ...
}
使用@Resource註解,必須添加JSR-250的依賴或者jar包。
相關文章
相關標籤/搜索