少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          1、使用<context:annotation-config />簡化配置 
                Spring2.1添加了一個新的context的Schema命名空間,該命名空間對注釋驅動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供元數據信息。要使元數據信息真正起作用,必須讓負責處理這些元數據的處理器工作起來。 
                AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些注釋元數據的處理器。但是直接在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的配置內容已經被移除,僅需要添加一行<context:component-scan />配置就解決所有問題了——Spring XML配置文件得到了極致的簡化(當然配置元數據還是需要的,只不過以注釋形式存在罷了)。<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表達式過濾一些類


          以正則表達式為例,我列舉一個應用實例:
          <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 />配置項不但啟用了對類包進行掃描以實施注釋驅動Bean定義的功能,同時還啟用了注釋驅動自動注入的功能(即還隱式地在內部注冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當使用<context:component-scan />后,就可以將<context:annotation-config />移除了.

          posted on 2015-03-23 19:38 abin 閱讀(403) 評論(0)  編輯  收藏 所屬分類: spring
          主站蜘蛛池模板: 嘉峪关市| 霸州市| 凤庆县| 方城县| 金坛市| 清苑县| 夹江县| 乡宁县| 南澳县| 白沙| 五原县| 简阳市| 勃利县| 江陵县| 新和县| 兴山县| 霞浦县| 沈丘县| 巫溪县| 綦江县| 平利县| 汕头市| 兖州市| 缙云县| 阿拉善右旗| 无棣县| 乌拉特前旗| 余姚市| 通州区| 湘阴县| 满城县| 河池市| 玉田县| 博爱县| 民乐县| 湖州市| 桂林市| 岳普湖县| 合肥市| 信宜市| 宜宾市|