基于XML的声明式事务控制

<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED"timeout="-1" read-only="false"/>

<tx:method>代表切点方法的事务参数的配置

  name:切点方法名称

  isolation:事务的隔离级别

  propagation:事务的传播行为

  timeout:超时时间

  read-only:是否只读

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 加载外部的properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    
    <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean id="accountDao" class="">
        <property name="jdbcTemplate" ref = "jdbcTemplate"></property>
    </bean>
    
    <!-- 目标对象  内部的方法就是切点 -->
    <bean id="accountService" class="">
      <property name="accountDao" ref="accountDao"></property>
  </bean>

    <!-- 配置平台事务管理器 -->
    <bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref = "dataSource"></property>
    </bean>


    <!--通知  事务的增强  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 设置事务的属性信息的 -->
        <tx:attributes>
            <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
            <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false"/>
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
            
        </tx:attributes>        
    </tx:advice>
    
    <!-- 配置事务的aop织入 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zl.service.impl.*.*(..))"/>
    </aop:config>
    
</beans>

 

posted @ 2022-11-30 18:52  Cuora  阅读(26)  评论(0)    收藏  举报