Spring學習筆記—最小化Spring XML配置

                                         spring學習筆記—最小化Spring XML配置

   自動裝配(autowiring)有助於減少甚至消除配置<property>元素和<constructor-arg>元素,讓Spring自動識別如何裝配Bean的依賴關係。
      自動檢測(autodiscovery)比自動裝配更進了一步,讓Spring能夠自動識別哪些類需要被配置成Spring Bean,從而減少對<bean>元素的使用。

1.自動裝配屬性

     1.1  4種類型的自動裝配

      ● byName——把與Bean的屬性具有相同名字(或者ID)的其他Bean自動裝配到Bean的對應屬性中。如果沒有跟屬性的名字相匹配的Bean,則該屬性不進行裝配。
      ● byType——把與Bean的屬性具有相同類型的其他Bean自動裝配到Bean的對應屬性中。如果沒有跟屬性的類型相匹配的Bean,則該屬性不被裝配。
      ● constructor——把與Bean的構造器入參具有相同類型的其他Bean自動裝配到Bean構造器的對應入參中。
      ● autodetect——首先嚐試使用constructor進行自動裝配。如果失敗,在嘗試使用byType進行自動裝配。
      
      byName自動裝配:
[html] view plain copy
 print?
  1. <bean id = "kenny" class = "com.ouc.test.springs.Instruments" autowire = "byName" >  
  2.    <property name = "song" value = "bells" />  
  3. </bean>  
    爲屬性自動裝配ID與該屬性的名字相同的Bean。
    
     byType自動裝配:
    如果Spring尋找到多個Bean,它們的類型與自動裝配的屬性類型都相匹配,Spring不會猜測哪一個Bean更適合自動裝配,而是會拋出異常。
    可以爲自動裝配標識一個首選Bean,或者可以取消某個Bean自動裝配的候選資格。爲了使用primary屬性,不得不將非首選Bean的primary屬性設置爲false。
[html] view plain copy
 print?
  1. <bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" primary = "false" />  
    primary屬性僅對標識首選Bean有意義。
    如果希望排除某些Bean,可以設置這些Bean的autowire-candidate屬性爲false。
[html] view plain copy
 print?
  1. <bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" autowire-candidate = "false" />  
   
    constructor自動裝配:
    如果要通過構造器注入來配置Bean,可以移除<constructor-arg>元素,由Spring在應用上下文中自動選擇Bean注入到構造器入參中。
[html] view plain copy
 print?
  1. <bean id = "saxophone" class="com.ouc.springs.test.Saxophone"  autowire ="constructor" />  
    
    最佳自動裝配:
[html] view plain copy
 print?
  1. <bean id = "saxophone" class="com.ouc.springs.test.Saxophone"  autowire ="autodetect" />  

   1.2 默認自動裝配

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.      http://www.springframework.org/schema/beans  
  6.      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.      default-autowire="byType" >  
  8.   </beans><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">  </span>  


2 .使用註解裝配

    Spring容器默認禁用註解裝配。
    啓用註解裝配最簡單的方式是使用Spring的context命名空間配置中的<context:annotation-config>元素。
    Spring 3 支持幾種不同的用於自動裝配的註解:
     ● Spring自帶的@Autowired註解;
     ● JSR-330的@Inject註解;
     ● JSR-250的@Resource註解。
    

   2.1 使用@Autowired

[html] view plain copy
 print?
  1. @Autowired  
  2. public void setInstrument(Instrument instrument){  
  3.   this.instrument = instrument;  
  4. }  
     Spring會嘗試對該方法執行byType自動裝配,可以使用@Autowired標註需要自動裝配Bean引用的任意方法。
     可以使用@Autowired註解直接標註屬性,並刪除setter方法:
[html] view plain copy
 print?
  1. @Autowired  
  2. private Instrument instrument;  
    1)可選的自動裝配:
        默認情況下,@Autowired所標註的屬性或參數必須是可以裝配的。如果沒有Bean可以裝配到@Autowired所標註的屬性或參數中,自動裝配就會失敗(拋出NoSuchBeanDefinitionException).
        可以通過設置@Autowired的required屬性爲false來配置自動裝配是可選的。
[html] view plain copy
 print?
  1. @Autowired(required=false)  
  2. private Instrument instrument;  
     2)限定歧義性的依賴
    @Qualifier註解縮小了自動裝配挑選候選Bean的範圍,通過指定Bean的ID把選擇範圍縮小到只剩下一個Bean。
[html] view plain copy
 print?
  1. @Autowired  
  2. @Qualifier("guitar")  
  3. private Instrument instrument;  
     3)創建自定義的限定器(Qualifier)
[html] view plain copy
 print?
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5. import org.springframework.beans.factory.annotation.Qualifier;  
  6.   
  7. @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Qualifier  
  10. public @interface StringedInstrument{}  

  2.2 藉助@Inject實現基於標準的自動裝配

[html] view plain copy
 print?
  1. @Inject  
  2. private Instrument instrument;  
     @Inject沒有required屬性。
     限定@Inject所標註的屬性。
[html] view plain copy
 print?
  1. @Inject  
  2. @Named("guitar")  
  3. private Instrument instrument;  
    @Named通過Bean的ID來標識可選擇的Bean,@Named實際上是一個使用@Qualifier註解所標註的註解。
    創建自定義的JSR-330 Qualifier
[html] view plain copy
 print?
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5. import javax.inject.Qualifier;  
  6.   
  7. @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})  
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Qualifier  
  10. public @interface StringedInstrument{}  

   2.3 在註解注入中使用表達式

[html] view plain copy
 print?
  1. @Value("#{systemProperties.myFavoriteSong}")  
  2. private String song;  


3.自動檢測Bean


     使用<context:component-scan>元素配置自動檢測。
[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.    <beans xmlns="http://www.springframework.org/schema/beans"  
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.      xmlns:context=="http://www.springframework.org/schema/context"  
  5.      xsi:schemaLocation="  
  6.       http://www.springframework.org/schema/beans  
  7.       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8.       http://www.springframework.org/schema/context  
  9.       http://www.springframework.org/schema/beans/spring-context-3.1.xsd" >  
  10.   
  11.       <context:component-scan base-package="com.ouc.springs.test" >  
  12.       </context:component-scan>  
  13.    </beans>  

    3.1爲自動檢測標註Bean

     默認情況下,<context:component-scan>查找使用構造型(stereotype)註解所標註的類,這些特殊的註解如下:
       @Component——通用的構造型註解,標識該類爲Spring組件。
       @Controller——標識將該類定義爲Spring MVC Controller。
       @Repository——標識將該類定義爲數據倉庫。
       @Service——標識將該類定義爲服務。
        使用@Component標註的任意自定義註解。

   3.2 過濾組件掃描

    通過爲<context:component-scan>配置<context:include-filter>和<context:exclude-filter>子元素,可以隨意調整掃描行爲。
[html] view plain copy
 print?
  1. <context:component-scan base-package="com.ouc.springs.test" >  
  2.         <context:include-filter type="assignable" expression="com.ouc.springs.tst.Instrument" />  
  3.         <context:exclude-filter type="annotation" expression="com.ouc.springs.tst.SkipIt" />  
  4. </context:component-scan>  
    

4.使用Spring基於Java的配置

    4.1 定義一個配置類

[html] view plain copy
 print?
  1. import org.springframework.context.annotation.Configuration;  
  2.   
  3. @Configuration  
  4. public class SpringIdolConfig{}  
    @Configuration註解會作爲一個標識告知Spring:這個類將包含一個或多個Spring Bean的定義。
    

   4.2 聲明一個簡單的Bean

[html] view plain copy
 print?
  1. @Bean  
  2. public Performer duke(){  
  3.   return new Juggler();  
  4. }  
      @Bean告知Spring這個方法將返回一個對象,該對象應該被註冊爲Spring應用上下文中的一個Bean,方法名將作爲該Bean的ID。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章