1. 程式人生 > >Spring配置事務

Spring配置事務

<?xml version="1.0" encoding="UTF-8"?>

<!-- 定義該文件是xml文件 該xml文件是1.0版本 該xml文件使用的是UTF-8的字元編碼格式 -->

<beans xmlns="http://www.springframework.org/schema/beans" 

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="

        http://www.springframework.org/schema/beans 

     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 

        http://www.springframework.org/schema/aop 

     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 

        http://www.springframework.org/schema/tx 

     http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

        ">

      <!--定義beans標籤,以及beans內部的自定義標籤的格式標準,該文件主要是配置事務,所有重點討論aop和tx兩個標籤。  -->   

    <!-- 配置事務管理器bean -->

    <bean id="transactionManager"

         class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <!-- 

       aop:config標籤的作用是配置一個切面,和需要攔截的類和方法.經過上邊的配置達到的效果是:

       org.hdth包下邊的以"Logic"結尾的class裡邊的方法名稱為任何名稱的方法,將按照txAdvice通知的規則執行事務。

    -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

    <!-- 

       tx:advice標籤的作用是:定義事務的屬性,或者可以說定義可以走事務方法的屬性。更準且的說是定義:什麼樣的方法對應走什麼樣的事物。

       其中,<tx:attributes>定義了事務的屬性,也就是定義了走事務方法的屬性。已知的屬性就是方法的名稱一個屬性。規則:

       定義方法的開始字元,並可以使用"*"號代替方法名稱的其他部分。當切面中的方法符合這些方法名稱的要求的時候,就會自動走事務處理。

       預設情況下,方法的只讀屬性應該是false(猜測的),對於查詢方法不需要起事務,為了節省資源,應該顯示配置查詢方法的只讀事務屬性為true。

       tx:advice的主要屬性:

       id:通知物件的標示id

       transaction-manager:指定該事務通知由哪個事務處理器處理。事務處理器的定義有很多種。

     -->

       <tx:attributes>

           <tx:method name="insert*"/>

           <tx:method name="update*"/>

           <tx:method name="save*"/>

           <tx:method name="add*"/>

           <tx:method name="remove*"/>

           <tx:method name="delete*"/>

           <tx:method name="modify*"/>

           <tx:method name="change*"/> 

           <tx:method name="reset*"/>

           <tx:method name="recycle*"/>

           <tx:method name="audit*"/>  

      <tx:method name="merger*"/>

                  

           <!-- 查詢方法的事務配置成只讀的 -->

           <tx:method name="get*"/>

           <tx:method name="list*"/>

           <tx:method name="find*" read-only="true"/>

           <tx:method name="load*" read-only="true"/>

           <tx:method name="search*" read-only="true"/>

           <tx:method name="*" read-only="true"/>

       </tx:attributes>

    </tx:advice>

    <!-- 在此之前,應該先 配置事務管理器,這部分的內容在兩外一個xml檔案配置了大概如下:(實際有很多中配置事物的方法,此處僅是一種而已。)

     配置事務管理器bean :

    <bean id="transactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    所以猜測:所有的spring配置文件內的東西都是通用的,spring會將所有的配置文件統一整理,就好比這些文件都寫在一個配置檔案中一樣。

     -->

    <aop:config proxy-target-class="true">

       <aop:advisor pointcut="execution(* org.hdht..*Logic.*(..))" advice-ref="txAdvice"/>

       <!-- aop:advisor的作用是構造一個通知,通知的的切面有pointcut指定。

       aop:advisor的屬性說明:

       pointcut:該屬性用來構造切面,切面的概念就是指定某一些後臺java類中的一些方法或者是對映後臺一部分的java類的一些方法,總之,最後從後臺

           單獨映射出一些java方法(重點在是方法上)。\

           格式:execution(* your.full.package.*(..)) 也可以簡化為:execution(* your..*pkname.*(..)) 

              說明:第一個*號不明白,似乎是必須有的,這個表示式中的兩個"."號似乎就是省略號,意思是模糊路徑。"*pkname"是說所有以"pkname"結尾的包。

              ".*(..)"的意思是,該包內的所有的方法,”(..)“表示帶有任意型別和個數的引數的方法。

       adviec-ref:該屬性用來說明已經指定的切面(就是一部分java類的一些方法)根據什麼"通知"規則使用spring的事務。實際應用過程中,主要是指定方法以什麼字元開頭。

        -->

    </aop:config>

    <bean class="org.hdht.commonweb.ApplicationContextHolder" lazy-init="false"/>

</beans>