配置事务管理器

编程式事务管理: 要修改原来的代码,加入事务管理代码 (侵入性 )— 不推荐,不使用
声明式事务管理:底层就是AOP的环绕通知, — 推荐

用XML配置方式添加事务管理(tx、aop约束)

第一步: 引入aop和tx 的名称空间,导入aop和tx 的jar

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
</beans>

第二步: 配置 transactionManager(spring提供 Around 通知 )
根据不同的持久层框架,配置不同的事务管理器,事务管理器需要依赖数据源信息

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        引入驱动--><property name="dataSource" ref="dataSource"></property></bean>

第三步: 配置切入点和切面
1、在spring的配置文件中,使用tx:advice配置通知信息,其实就是哪些需要方法需要增强,事务管理也是aop的应用

<tx:advice><tx:attributes><tx:method name="具体的方法名"/></tx:attributes></tx:advice>

2、使用aop:config来配置切入点和切面信息

<!--    2、配置切入点,你要对哪一个方法进行增强--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="updateMoney"/></tx:attributes></tx:advice><!--    3、配置切面--><aop:config>
<!--        配置切入点表达式--><aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>
<!--        切面配置,切入点和通知进行一个结合--><aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor></aop:config>

用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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"><!--    配置注解扫描包:扫描com.sky包下的所有组件--><context:component-scan base-package="com.sky"></context:component-scan><!--    核心思想:将Mybatis的所有对象全部都交给Spring管理--><!--    引入数据源信息--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><!--        location值就需要加上classpath, classpath = src--><property name="location" value="classpath:db.properties"></property></bean><!--    配置数据源,数据库链接池,dbcp,c3p0--><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${driver}"></property><property name="url" value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property></bean><!--    sqlSessionFactory:加载工厂--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!--        configLocation:加载 Mybatis 的配置文件--><property name="configLocation" value="classpath:MybatisConfig.xml"></property><!--        扫描加载所有的mapper.xml文件--><property name="mapperLocations" value="classpath:com/sky/mapperxml/*.xml"></property></bean><!--    sqlSession--><bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg></bean><!--    配置接口的扫描包--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.sky.mapper"></property></bean><!--    *****-->
<!--    *****-->
<!--    1、配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        引入驱动--><property name="dataSource" ref="dataSource"></property></bean><!--    2、配置切入点,你要对哪一个方法进行增强--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="updateMoney"/></tx:attributes></tx:advice><!--    3、配置切面--><aop:config>
<!--        配置切入点表达式--><aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>
<!--        切面配置,切入点和通知进行一个结合--><aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor></aop:config></beans>

注解配合方式添加事务管理 @Transactional

第一步: 配置注解驱动事务管理

<!--    1、配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        引入驱动--><property name="dataSource" ref="dataSource"></property></bean>

第二步: 在需要管理事务的方法或者类上面 添加@Transactional 注解

<!--    2、事务管理器的注解驱动配置,加了这个配置就能识别 Transactional 注解--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

用注解配合方式添加事务管理代码

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"><!--    配置注解扫描包:扫描com.sky包下的所有组件--><context:component-scan base-package="com.sky"></context:component-scan><!--    核心思想:将Mybatis的所有对象全部都交给Spring管理--><!--    引入数据源信息--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><!--        location值就需要加上classpath, classpath = src--><property name="location" value="classpath:db.properties"></property></bean><!--    配置数据源,数据库链接池,dbcp,c3p0--><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${driver}"></property><property name="url" value="${url}"></property><property name="username" value="${username}"></property><property name="password" value="${password}"></property></bean><!--    sqlSessionFactory:加载工厂--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!--        configLocation:加载 Mybatis 的配置文件--><property name="configLocation" value="classpath:MybatisConfig.xml"></property><!--        扫描加载所有的mapper.xml文件--><property name="mapperLocations" value="classpath:com/sky/mapperxml/*.xml"></property></bean><!--    sqlSession--><bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg></bean><!--    配置接口的扫描包--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.sky.mapper"></property></bean><!--    *****-->
<!--    *****-->
<!--    1、配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        引入驱动--><property name="dataSource" ref="dataSource"></property></bean><!--    2、事务管理器的注解驱动配置,加了这个配置就能识别 Transactional 注解--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven><!--&lt;!&ndash;    配置切入点,你要对哪一个方法进行增强&ndash;&gt;-->
<!--    <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--        <tx:attributes>-->
<!--            <tx:method name="updateMoney"/>-->
<!--        </tx:attributes>-->
<!--    </tx:advice>--><!--&lt;!&ndash;    配置切面&ndash;&gt;-->
<!--    <aop:config>-->
<!--&lt;!&ndash;        配置切入点表达式&ndash;&gt;-->
<!--        <aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>-->
<!--&lt;!&ndash;        切面配置,切入点和通知进行一个结合&ndash;&gt;-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor>-->
<!--    </aop:config>--></beans>

事务管理器增强的方法

package com.sky.service;import com.sky.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class AccountService {@Autowiredprivate AccountMapper accountMapper;@Transactional  //  添加事务管理public void updateMoney(String numberOut,String numberIn,double money){accountMapper.getMoneyOut(numberOut,money);System.out.println(1/0);    //  这行代码有异常accountMapper.getMoneyIn(numberIn,money);}
}

问题: XML配置方式和注解配置方式 进行事务管理 哪种用的多?

XML方式 和注解都在使用:
使用@Transactional注解进行事务管理,方便,不过配置太分散, 使用XML进行事务管理 属性集中配置,便于管理和维护

    <tx:advice id="txadvice"><tx:attributes>
<!--            例:增强所有add开头的方法--><tx:method name="add*"/><tx:method name="save*"/><tx:method name="update*"/><tx:method name="delete*"/><tx:method name="get*" read-only="true"/><tx:method name="query*" read-only="true"/><tx:method name="find*" read-only="true"/></tx:attributes></tx:advice>

Spring事务管理器配置两种配置方法,使用方法相关推荐

  1. Spring事务管理器的配置和使用

    Spring事务管理器的配置和使用 1.为什么要配置spring事务管理器. 在将spring和hibernate结合之后,我们需要将事务管理交给spring管理.以保证数据的安全型,避免脏数据的出现 ...

  2. spring事务管理器的作用_【面试必问】Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

  3. Spring事务管理器分类

    Spring并不直接管理事务,事实上,它是提供事务的多方选择.你能委托事务的职责给一个特定的平台实现,比如用JTA或者是别的持久机制.Spring的事务管理器可以用下表表示: 事务管理器的实例 目标 ...

  4. java中事务实例,Java Spring 事务管理器入门例子教程(TranscationManager)

    注:阅读本文之前请务必有上文的基础 本文我们使用的事务管理器(TranscationManager)的作用是保证一组数据库操作的原子性.保证在操作时,如果出现异常,事务管理器会将状态恢复到进行本组操作 ...

  5. Java中BorderLayout布局管理器的两种排列实现方式

    java中Frame类默认的布局管理器为BorderLayout,其主要是将Frame窗口分为东西南北中五个区域,每个区域仅限于放一个组件,如加入多个,前免得会被覆盖,解决方法为:可以在一个区域中加入 ...

  6. python中实现上下文管理器的两种方法

    上下文管理器: python中实现了__enter__和__exit__方法的对象就可以称之为上下文管理器 实现方法一举例: def File(object): def __init__(self, ...

  7. mybatis源码分析之事务管理器

    2019独角兽企业重金招聘Python工程师标准>>> 上一篇:mybatis源码分析之Configuration 主要分析了构建SqlSessionFactory的过程中配置文件的 ...

  8. spring事务管理的两种方式

    一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...

  9. Spring事务管理 与 SpringAOP

    1,Spring事务的核心接口 Spring事务管理的实现有许多细节,如果对整个接口框架有个大体了解会非常有利于我们理解事务,下面通过讲解Spring的事务接口来了解Spring实现事务的具体策略.  ...

最新文章

  1. python3.6.5安装tensorflow_Win10下用Anaconda安装TensorFlow(图文教程)
  2. Java基础(三)——反射、代理
  3. IDEA自动生成序列化ID
  4. Spring 定时器结合线程池
  5. css常用或不熟悉的
  6. excel表格数字显示不全_表格技巧—Excel里身份证号码显示不全的多种解决办法...
  7. JAVA 相关书籍推荐(全)
  8. erlang的又一力作——英雄联盟聊天服务器
  9. 大数据Hadoop技术的发展历史与未来前景
  10. HDU 2154 跳舞毯
  11. 服务器装系统步骤图解win7,Win7原版系统安装教程(超详细图文版)
  12. jQuery调用JSON数据学习第一天
  13. python实现微信付款码支付(刷卡支付)(纯python)
  14. 「文献解读」利用基因沉默和过表达技术研究棉花的基因功能
  15. 复制公司代码后更改会计科目表(OB62),报错:更改会计科目表前重置公司码数据-OBR1/OBR2
  16. python编程求长方体体积_python编程求长方体体积_【Python编程特训连载80】答案公布:圆柱体体积计算...
  17. 从C程序到bin文件
  18. 一文看懂:芯片IC的封装/测试流程
  19. 新一代ERPII 企业销售、采购及财务一体化高效管理的利器
  20. vue项目web前端登录页数字验证码 登录流程

热门文章

  1. js打开新窗口的各种方法
  2. bim学习—— 第7章 放置首层门窗
  3. Hive源码阅读--导读
  4. PTA 7-20 打印九九口诀表
  5. 计算机硬盘虚拟内存是什么,内存磁盘(RAM作为虚拟硬盘)计算机加速Primo Ramdisk设置教程(详细)...
  6. java计算机毕业设计航空公司机票预订管理系统源码+mysql数据库+系统+lw文档+部署
  7. ecshop仿聚美优品商业模板+特卖+团购+客服+批量传图,美容护肤类专用模版
  8. 【计组理论期末考试模拟题】21级计科专业计算机组成原理
  9. 通过onvif抓取海康摄像头图片,以及解决海康摄像头抓取图片需要验证问题,实现摄像头一段时间换一个地方的同时抓取一张图片。
  10. Picasso使用问题记录 - 加载local camera image 失败