Spring事务管理器配置两种配置方法,使用方法
配置事务管理器
编程式事务管理: 要修改原来的代码,加入事务管理代码 (侵入性 )— 不推荐,不使用
声明式事务管理:底层就是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><!--<!– 配置切入点,你要对哪一个方法进行增强–>-->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="updateMoney"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>--><!--<!– 配置切面–>-->
<!-- <aop:config>-->
<!--<!– 配置切入点表达式–>-->
<!-- <aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>-->
<!--<!– 切面配置,切入点和通知进行一个结合–>-->
<!-- <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事务管理器配置两种配置方法,使用方法相关推荐
- Spring事务管理器的配置和使用
Spring事务管理器的配置和使用 1.为什么要配置spring事务管理器. 在将spring和hibernate结合之后,我们需要将事务管理交给spring管理.以保证数据的安全型,避免脏数据的出现 ...
- spring事务管理器的作用_【面试必问】Spring中的事务管理详解
在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...
- Spring事务管理器分类
Spring并不直接管理事务,事实上,它是提供事务的多方选择.你能委托事务的职责给一个特定的平台实现,比如用JTA或者是别的持久机制.Spring的事务管理器可以用下表表示: 事务管理器的实例 目标 ...
- java中事务实例,Java Spring 事务管理器入门例子教程(TranscationManager)
注:阅读本文之前请务必有上文的基础 本文我们使用的事务管理器(TranscationManager)的作用是保证一组数据库操作的原子性.保证在操作时,如果出现异常,事务管理器会将状态恢复到进行本组操作 ...
- Java中BorderLayout布局管理器的两种排列实现方式
java中Frame类默认的布局管理器为BorderLayout,其主要是将Frame窗口分为东西南北中五个区域,每个区域仅限于放一个组件,如加入多个,前免得会被覆盖,解决方法为:可以在一个区域中加入 ...
- python中实现上下文管理器的两种方法
上下文管理器: python中实现了__enter__和__exit__方法的对象就可以称之为上下文管理器 实现方法一举例: def File(object): def __init__(self, ...
- mybatis源码分析之事务管理器
2019独角兽企业重金招聘Python工程师标准>>> 上一篇:mybatis源码分析之Configuration 主要分析了构建SqlSessionFactory的过程中配置文件的 ...
- spring事务管理的两种方式
一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...
- Spring事务管理 与 SpringAOP
1,Spring事务的核心接口 Spring事务管理的实现有许多细节,如果对整个接口框架有个大体了解会非常有利于我们理解事务,下面通过讲解Spring的事务接口来了解Spring实现事务的具体策略. ...
最新文章
- 【踩坑之旅】-webpack (v4.8.1) + vue-cli (v2.5.3)升级准备
- 实现在tabcontrol里面拖拽tabpage来设置tabpage的位置
- idea部署web项目,能访问jsp,访问Servlet却出现404错误的解决方法汇总
- 对内存重叠的深入认识
- 重新启动系统中的network服务器,linux系统调优-Network
- android 9.0 c7Pro,三星c7pro和vivo x9哪个好?三星Galaxy c7 pro和vivo x9区别对比评测
- deep_sort文章阅读(一)yolo的训练:PART1: 安装opencv(为了make时opencv=1)
- Unified diagnostic services (UDS)
- 理解virt、res、shr之间的关系(linux系统篇)
- Flutter项目网络图片调试模式正常,打包后不显示(Android)
- 牡丹的冬季修剪及管理方法
- 【第六篇】Qt学习与使用---在qt中打印PDF文件(不是生成PDF)
- 常见的概率公式及其推导(马尔科夫HMM系列课程拓展)
- 趋势顶底指标公式 通达信趋势顶底主图选股指标 清晰的趋势顶底详解
- node JS獲取GPS_node学习笔记(三十八)
- 远程互动 gk服务器,不要错过!GKUI APP远程控制教程
- android 三维软件 cad,CAD实例教程:快速设计呆萌的安卓机器人
- 马悦凌:从初级护士到“民间奇医”[3]
- Ubuntu下C语言程序编写与运行
- 解决GitHub频繁要求verify email的问题