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>



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值