少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          1、使用<context:annotation-config />簡化配置 
                Spring2.1添加了一個新的context的Schema命名空間,該命名空間對注釋驅(qū)動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供元數(shù)據(jù)信息。要使元數(shù)據(jù)信息真正起作用,必須讓負責(zé)處理這些元數(shù)據(jù)的處理器工作起來。 
                AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些注釋元數(shù)據(jù)的處理器。但是直接在Spring配置文件中定義這些Bean顯得比較笨拙。Spring為我們提供了一種方便的注冊這些BeanPostProcessor的方式,這就是<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-2.5.xsd  
              http://www.springframework.org/schema/context  
              http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
              <context:annotation-config />  
          </beans>  

          <context:annotationconfig />將隱式地向Spring容器注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor這4個BeanPostProcessor。

          2、使用<context:component-scan />讓Bean定義注解工作起來
          <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-2.5.xsd  
              http://www.springframework.org/schema/context  
              http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
              <context:component-scan base-package="com.kedacom.ksoa" />  
          </beans>  

          這里,所有通過<bean>元素定義Bean的配置內(nèi)容已經(jīng)被移除,僅需要添加一行<context:component-scan />配置就解決所有問題了——Spring XML配置文件得到了極致的簡化(當(dāng)然配置元數(shù)據(jù)還是需要的,只不過以注釋形式存在罷了)。<context:component-scan />的base-package屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。 
          < context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式: 
          過濾器類型 表達式范例 說明
          注解 org.example.SomeAnnotation將所有使用SomeAnnotation注解的類過濾出來
          類名指定 org.example.SomeClass過濾指定的類
          正則表達式 com\.kedacom\.spring\.annotation\.web\..*通過正則表達式過濾一些類
          AspectJ表達式 org.example..*Service+通過AspectJ表達式過濾一些類


          以正則表達式為例,我列舉一個應(yīng)用實例:
          <context:component-scan base-package="com.casheen.spring.annotation">  
              <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />  
          </context:component-scan>  

          值得注意的是<context:component-scan />配置項不但啟用了對類包進行掃描以實施注釋驅(qū)動Bean定義的功能,同時還啟用了注釋驅(qū)動自動注入的功能(即還隱式地在內(nèi)部注冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當(dāng)使用<context:component-scan />后,就可以將<context:annotation-config />移除了.

          posted on 2015-03-23 19:38 abin 閱讀(408) 評論(0)  編輯  收藏 所屬分類: spring
          主站蜘蛛池模板: 乐山市| 永年县| 逊克县| 吉首市| 眉山市| 都昌县| 昌江| 沙洋县| 调兵山市| 桂林市| 花莲市| 乌兰浩特市| 江口县| 邻水| 鸡东县| 三明市| 岳西县| 嘉祥县| 清远市| 通城县| 逊克县| 新宾| 佛坪县| 曲松县| 莲花县| 日喀则市| 秭归县| 芜湖市| 铜川市| 泰宁县| 子洲县| 白山市| 白朗县| 永清县| 高青县| 乐山市| 惠水县| 北京市| 鹤岗市| 正镶白旗| 民勤县|