简述

1、Spring 的声明式事务管理在底层是建立在 AOP 的基础上。其本质是在方法前后进行拦截,然后在目标方法开始之前创建一个事务,在执行这目标方法结束后,根据执行情况提交或进行回滚事务。

2、声明式事务最大的优点就是不需通过编程的方式而进行管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明,便可将事务规则应用到业务逻辑中。

3、声明式事务不足的地方在于,与编程式事务相比,只能作用到方法级别,无法像编程式事务那样可以作用到代码块级别。

XML方式

<!-- 配置事务管理器================= -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置事务管理的规则,如隔离级别,超时信息,传播行为,是否只读, -->
<!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/> -->
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- aop的配置=================== -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* zcc.spring_tx.demo2.accountServiceImp1.*(..))" id="pointcut1"/>
<!-- 配置切入面,以前刚学面向切面编程的时候需要手动编写一个切面类,但是事务spring支持了,不需要自己手动编写-->
<!-- <aop:aspect ref="">
<aop:before method=""/>
</aop:aspect> -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> </aop:config>

注解方式

  <!-- 配置事务管理器================= -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 开启注解事务================= -->
<tx:annotation-driven transaction-manager="transactionManager"/>

在业务层上添加注解,注解里面也可以添加属性,比如

@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)

详细代码如下:

tx2.xml

<?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: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.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置Service================ -->
<bean id="accountService"
class="zcc.spring_tx.demo2.accountServiceImp1">
<property name="dao" ref="accountDao"></property>
</bean> <!-- 配置Dao===================== -->
<bean id="accountDao" class="zcc.spring_tx.demo2.accountDaoImp1">
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置连接池和jdbc模板 ===================== -->
<context:property-placeholder
location="classpath:jdbc.properties" /> <!-- 配置c3p0连接池============ -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- 配置jdbc模板================ -->
<!-- <bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
==属性注入==
<property name="dataSource" ref="dataSource"></property>
</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>
<!-- 配置事务管理的规则 -->
<!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/> -->
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- aop的配置=================== -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* zcc.spring_tx.demo2.accountServiceImp1.*(..))" id="pointcut1"/>
<!-- 配置切入面,以前刚学aop的时候需要手动编写一个切面类,但是事务spring支持了,不需要自己手动编写-->
<!-- <aop:aspect ref="">
<aop:before method=""/>
</aop:aspect> -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> </aop:config>
</beans>

tx3.xml

<?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: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.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置Service================ -->
<bean id="accountService"
class="zcc.spring_tx.demo3.accountServiceImp1">
<property name="dao" ref="accountDao"></property>
</bean> <!-- 配置Dao===================== -->
<bean id="accountDao" class="zcc.spring_tx.demo3.accountDaoImp1">
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置连接池和jdbc模板 ===================== -->
<context:property-placeholder
location="classpath:jdbc.properties" /> <!-- 配置c3p0连接池============ -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></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="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 开启注解事务=================== -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

补充:

重点掌握声明式配置

xml的,配置事务管理器,<tx:advice>,<tx:attributes>,<tx:method>,<aop:config>,,<aop:pointcut>,<aop:advisor>

注解方式的:<tx:annotation-driven transaction-manager="transactionManager"/>,在业务层上一定要记得加事务的注解

最后,看到一篇写的很不错的关于spring事务的博客,在此附上地址,总结的很好:https://blog.csdn.net/trigl/article/details/50968079

Spring事务管理2--声明式的更多相关文章

  1. Spring事务管理之声明式事务管理-基于注解的方式

    © 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(TransactionProxyFactoryBean)管理模拟转账过程 数据库准备 -- 创建表 CREAT ...

  2. Spring事务管理之声明式事务管理-基于AspectJ的XML方式

    © 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(AspectJ)管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( ` ...

  3. Spring事务管理之声明式事务管理:基于TransactionProxyFactoryBean的方式

    © 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(TransactionProxyFactoryBean)管理模拟转账过程 数据库准备 -- 创建表 CREAT ...

  4. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  5. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  6. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  7. spring事务配置,声明式事务管理和基于@Transactional注解的使用(转载)

    原文地址:http://blog.csdn.net/bao19901210/article/details/41724355 事务管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的 ...

  8. spring笔记--事务管理之声明式事务

    事务简介: 事务管理是企业级应用开发中必不可少的技术,主要用来确保数据的完整性和一致性, 事务:就是一系列动作,它们被当作一个独立的工作单元,这些动作要么全部完成,要么全部不起作用. Spring中使 ...

  9. Spring框架的事务管理之声明式事务管理的类型

    1. 声明式事务管理又分成两种方式 * 基于AspectJ的XML方式(重点掌握)(具体内容见“https://www.cnblogs.com/wyhluckdog/p/10137712.html”) ...

  10. 转:全面分析 Spring 的编程式事务管理及声明式事务管理

    转:from: https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/

随机推荐

  1. 【golang-GUI开发】qt之signal和slot(二)

    上一篇文章里我们详细介绍了signal的用法. 今天我们将介绍slot的使用.在qt中slot和signal十分相像,这次我们将实现一个能显示16进制数字的SpinBox,它继承自QSpinbox并重 ...

  2. 工作中常用Windows快捷键整理(1)-快速关闭网页

    打开桌面 win+D,显示桌面快捷键,不会关闭浏览器页面,是显示桌面. 关闭当前打开的所有标签页 Alt+F4,关闭当前打开的所有浏览器标签页. 关闭当前打开的标签页 Ctrl+W,关闭当前打开的标签 ...

  3. C#中saveFileDialog(另存为)保存图片文件

    弹出另存为提示框保存图片文件: //用户自由选择指定路径保存文件            SaveFileDialog savedialog = new SaveFileDialog();        ...

  4. 31.QPainter-rotate()函数分析-文字旋转不倾斜,图片旋转实现等待

    在上章和上上上章: 28.QT-QPainter介绍 30.QT-渐变之QLinearGradient. QConicalGradient.QRadialGradient 学习了QPainter基础绘 ...

  5. 23.C++- 继承的多种方式、显示调用父类构造函数、父子之间的同名函数、virtual虚函数

     上章链接: 22.C++- 继承与组合,protected访问级别 继承方式 继承方式位于定义子类的”:”后面,比如: class Line : public Object //继承方式是publi ...

  6. Netty 系列六(编解码器).

    一.概念 网络传输的单位是字节,如何将应用程序的数据转换为字节,以及将字节转换为应用程序的数据,就要说到到我们该篇介绍的编码器和解码器. 将应用程序的数据转换为网络格式,以及将网络格式转换为应用程序的 ...

  7. JVM 垃圾回收机制

    首先JVM的内存结构包括五大区域: 程序计数器.虚拟机栈.本地方法栈.方法区.堆区.其中程序计数器.虚拟机栈和本地方法栈3个区域随线程启动与销毁, 因此这几个区域的内存分配和回收都具有确定性,不需要过 ...

  8. 10个常见的JavaScript BUG

    译者按: 安全起见,在开发中我基本不用==. 原文: 10 COMMON JAVASCRIPT BUGS AND HOW TO AVOID THEM 译者: Fundebug 为了保证可读性,本文采用 ...

  9. 虚拟机与Docker有何不同?

    译者按: 各种虚拟机技术开启了云计算时代:而Docker,作为下一代虚拟化技术,正在改变我们开发.测试.部署应用的方式.那虚拟机与Docker究竟有何不同呢? 原文: Comparing Virtua ...

  10. crontab工具安装和检查

    什么是crontab?crontab 是一个用于设置周期性执行任务的工具 重启crond守护进程 systemctl restart crond 查看当前crond状态 systemctl statu ...