zoukankan      html  css  js  c++  java
  • Spring中事务的传播特性

    15、Spring中事务管理

    Spring中的事务分为2类

    • 声明式事务
      • 结合aop实现
      • 不会破坏原有的代码
    • 编程式事务
      • 在代码中添加事务
      • 会破坏原有的代码

    声明式事务的实现:

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!-- 引入外部属性文件 -->
        <context:property-placeholder location="jdbc.properties"/>
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
            <property name="driverClass" value="${mysql.driver}"/>
            <property name="jdbcUrl" value="${mysql.url}"/>
            <property name="user" value="${mysql.username}"/>
            <property name="password" value="${mysql.password}"/>
        </bean>
    
    <!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
    <!--        <property name="driverClassName" value="${mysql.driver}"/>-->
    <!--        <property name="url" value="${mysql.url}"/>-->
    <!--        <property name="username" value="${mysql.username}"/>-->
    <!--        <property name="password" value="${mysql.password}"/>-->
    <!--    </bean>-->
    
        <!-- 配置SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource"  ref="dataSource"/>
            <!-- 关联mybatis配置文件 -->
            <property name="configLocation" value="mybatis-config.xml"/>
            <!-- 添加映射器 -->
            <property name="mapperLocations" value="classpath:com/ch/dao/*.xml"/>
        </bean>
    
        <!-- 通过配置好的SQLSessionFactory配置SqlSessionTemplate -->
        <!-- 没有set方法,只能通过构造器注入 -->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    
        <!-- 注入实现类 -->
    <!--    <bean id="userDaoImpl" class="com.ch.dao.impl.UserDaoImpl">-->
    <!--        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>-->
    <!--    </bean>-->
    
        <bean id="userDaoImpl2" class="com.ch.dao.impl.UserDaoImpl2">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
    
        <!-- 开启事务:即配置声明式事务 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- 配置事务的通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <!-- 哪些方法需要配置事务 -->
            <tx:attributes>
                <!-- 表示为所有方法都添加事务
                    name:对应的方法名,*表示所有,也可以单独配置某一方法
                    propagation:配置事务的传播特性
                 -->
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
    
        <!-- 配置aop,进行事务的切入-->
        <aop:config>
            <!-- 切入点的配置:com.ch.dao.*.*(..)表示dao包下所有类所有方法 -->
            <aop:pointcut id="pointcut" expression="execution(* com.ch.dao.*.*(..))"/>
            <!-- 定义通知器,需要在XX(pointcut)对应的方法切入XX(txAdvice)通知 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
        </aop:config>
    
    </beans>
    

    关于propagation事务的传播特性参考:

    https://blog.csdn.net/sayoko06/article/details/79164858

  • 相关阅读:
    【design pattern】代理模式
    Java IO(十七)FIleReader 和 FileWriter
    Java IO(十六)InputStreamReader 和 InputStreamWriter
    Java IO(十五)FilterReader 和 FilterWriter、FilterReader 子类 PushBackReader
    Java IO(十四) CharArrayReader 和 CharArrayWriter
    Java IO(十三)PipedReader 和 PipedWriter
    Java IO(十二) 字符流 Writer 和 Reader
    Java IO(十一) DataInputStream 和 DataOutputStream
    Java IO(十) BufferedInputStream 和 BufferedOutputStream
    Java IO(九)FilterInputStream 和 FilterOutputStream
  • 原文地址:https://www.cnblogs.com/IT_CH/p/13515183.html
Copyright © 2011-2022 走看看