[刘阳Java]_Spring对Transaction的支持_第12讲

Posted 刘阳Java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[刘阳Java]_Spring对Transaction的支持_第12讲相关的知识,希望对你有一定的参考价值。

Spring对数据库事务管理是非常优秀,它利用AOP方式来管理数据库操作的事务。优点:主要体现了业务功能设计不会和事务代码耦合。在使用Spring对Transaction支持中建议采用声明式事务管理来完成。

1. Spring对Transaction设计的代码步骤如下(重点关注配置文件编写,我们以JDBC事务管理来给大家进行阐述

  • 配置数据源
  • 配置事务管理器,DataSourceTransactionManager。这是事务管理器针对管理JDBC事务
  • 通过AOP让service包下所有Bean的所有方法拥有事务
  • 配置JdbcTemplate
  • 配置context:component-scan(扫描加入Spring注解的类),将标注Spring注解的类自动转化Bean,同时完成Bean的注入

2. 配置文件代码如下

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.gxaedu.dao"/>
    <context:component-scan base-package="com.gxaedu.service"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" 
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/sampledb" 
        p:username="root"
        p:password="1234" />

    <!-- 配置Jdbc模板  -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
        p:dataSource-ref="dataSource" />
        
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />
        
    <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod"
            expression=" execution(* com.gxaedu.service..*(..))" />
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
</beans>

 注意上面的配置文件有几个关键点,下面我会逐一的介绍一下

  • proxy-target-class
  • tx:advice, tx:attributes, tx:method

3. 我们也不知道上面的配置是否真正的成功,请按照下面的方式进行测试。这一点大家要注意

  • 注意如果配置事务是接口代理则测试程序是接口
  • 交叉测试,一个Service里面有两个以上的Dao对象。让第一个Dao对象SQL语句能够执行成功,但是第二个Dao对象SQL语句操作失败,看事务能不能管理Service中的方法,让数据全部回滚

以上是关于[刘阳Java]_Spring对Transaction的支持_第12讲的主要内容,如果未能解决你的问题,请参考以下文章

[刘阳Java]_Spring入门_第1讲

[刘阳Java]_Spring整合Servlet补充_第14讲

[刘阳Java]_Spring AOP基于XML配置介绍_第9讲

[刘阳Java]_Spring常用注解介绍_第6讲

[刘阳Java]_Spring相关配置介绍_第5讲

[刘阳Java]_Spring AOP入门_第7讲