SSM中事务的配置模板
2024-10-11 01:30:47
Spring-tx.xml
配置思路:
1. 声明事务管理器DataSourceTransactionManager
,并注入数据源dataSource
属性
2.配置事务增强<tx:advice>
,声明事务管理器transaction-manager=""
,默认值为transaction-manager="transactionManager"
,设置事务属性<tx:attributes>
,声明要为哪些方法配置事务<tx:method name=""/>
,以及定义方法的一些属性
3. 配置切面<aop:config>
,首先通过切入点表达式声明切入点位置,然后关联事务通知和切入点
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="transaction" transaction-manager="transactionManager">
<!-- 事务属性 -->
<tx:attributes>
<tx:method name="*"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<!--增删改 -->
<tx:method name="insert*" timeout="5000" rollback-for="java.lang.Exception"/>
<tx:method name="add*" timeout="5000"/>
<tx:method name="update*" timeout="5000"/>
<tx:method name="delete*" timeout="5000"/>
</tx:attributes>
</tx:advice>
<!-- 配置版的事务切面 -->
<aop:config>
<!-- *任意返回值 impl包下 .*任意类 .*任意方法 (..)任意参数 -->
<aop:pointcut id="pointcut" expression="execution(* com.juyss.service.impl.*.*(..))"/>
<!-- advice-ref:关联增强事务 pointcut-ref:关联事务的切入点 -->
<aop:advisor advice-ref="transaction" pointcut-ref="pointcut"/>
</aop:config>
</beans>
最新文章
- [spring源码学习]七、IOC源码-Context
- 制作简单的2D物理引擎(零)
- Windows Server 2016正式版14393英文版ISO镜像下载:_X64FRE_ZH-CN.ISO
- Kali Linux渗透基础知识整理(四):维持访问
- java中的负数的问题
- [FMS]FMS流媒体服务器onStatus介绍说明
- Java读数据是的编码问题。
- HDU 1014:Uniform Generator
- EJB总结
- 最新Android 出现Please ensure that adb is correctly located at问题的解决方法
- 分析Ext2文件系统结构。
- 一步一步在Windows中使用MyCat负载均衡 下篇
- JS的DOM操作及动画
- Mysql 忘记root密码后修改root密码
- hdu 5446(中国剩余+lucas+按位乘)
- 介绍一种很棒的wince 如何替换系统声音的方法
- Matlab中小语法点总结(更新中)
- mysqlsla快速入门
- Linux 标准文件描述符
- C语言之Bit-wise Operation和Logical Operation