Spring 在xml配置里配置事务
事先准备:
配置数据源对象
用<bean>实例化各个业务对象。
1.配置事务管理器。
<bean id="transactionManager" class="org.springframework.jdbc.datasourceManager">
<property name="datasource" ref="datasource"></property>
</bean>
2.配置事务属性
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="方法名" propagation="REQUIRES_NEW"/> <!--新开事务-->
<tx:method name="*"/> <!--使用原有事务-->
</tx:attributes>
</tx:advice>
3.配置事务切入点,注入事务属性
<aop:config>
<aop:pointcut expression="execution(.......)" id="txPointCut"/>
<aop:advisor advice-ref="txtAdvice" pointcut-ref="txtPointCut"/>
</aop:config>
实例:
准备工作:导入c3p0、Spring框架、Mysql、AOP的jar包,并配置好。
db.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb
user=root
password=
minPoolSize=5
maxPoolSize=20
initialPoolSize=5
三个接口
package com.itnba.maya.dao;
public interface IInfoDao {
public void delete(String code);
}
package com.itnba.maya.dao;
public interface IWorkDao {
public void deleteInfocode(String code);
}
package com.itnba.maya.dao;
public interface IInfoService {
public void delete(String code);
}
接口的实现类
package com.itnba.maya.daoimp; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import com.itnba.maya.dao.IInfoDao; public class InfoDao implements IInfoDao { private JdbcTemplate j;
public JdbcTemplate getJ() {
return j;
}
public void setJ(JdbcTemplate j) {
this.j = j;
}
@Override
public void delete(String code) {
// 故意设置一个错误
if(code.equals("p008")){
int n=1/0;
} String sql="delete from info where code=?";
j.update(sql,code);
} }
package com.itnba.maya.daoimp; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import com.itnba.maya.dao.IWorkDao; public class WorkDao implements IWorkDao { private JdbcTemplate j; public JdbcTemplate getJ() {
return j;
} public void setJ(JdbcTemplate j) {
this.j = j;
} public void deleteInfocode(String code) { String sql="delete from work where infocode=?";
j.update(sql,code);
} }
package com.itnba.maya.daoimp; import com.itnba.maya.dao.IInfoDao;
import com.itnba.maya.dao.IInfoService;
import com.itnba.maya.dao.IWorkDao; public class InfoService implements IInfoService { private IInfoDao infoDao;
public IInfoDao getInfoDao() {
return infoDao;
}
public void setInfoDao(IInfoDao infoDao) {
this.infoDao = infoDao;
}
public IWorkDao getWorkdao() {
return workdao;
}
public void setWorkdao(IWorkDao workdao) {
this.workdao = workdao;
}
private IWorkDao workdao;
public void delete(String code) { infoDao.delete(code);
workdao.deleteInfocode(code); } }
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:tx="http://www.springframework.org/schema/tx"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 引入db.properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 生成连接池 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
</bean>
<!-- 生成JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="j">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置实体类 -->
<bean class="com.itnba.maya.daoimp.InfoDao" id="infoDao">
<property name="j" ref="j"></property>
</bean>
<bean class="com.itnba.maya.daoimp.WorkDao" id="workDao">
<property name="j" ref="j"></property>
</bean>
<bean class="com.itnba.maya.daoimp.InfoService" id="service">
<property name="infoDao" ref="infoDao"></property>
<property name="workdao" ref="workDao"></property>
</bean> <!-- 配置事务管理器 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/><!-- *是对所有方法都加 -->
</tx:attributes>
</tx:advice>
<!-- 用切点把事务切进去 -->
<aop:config>
<aop:pointcut expression="execution(* com.itnba.maya.daoimp..*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config> </beans>
mian函数测试事务有没有生效:
package com.itnba.maya.daoimp; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.itnba.maya.dao.IInfoService; public class Test {
private static ApplicationContext context=null; private static IInfoService infoservice=null; static{
context=new ClassPathXmlApplicationContext("beans.xml");
infoservice=(IInfoService) context.getBean("service"); } public static void main(String[] args) { infoservice.delete("p008"); } }
结果除0错误,数据回滚,数据库并没有删除。说明配置的事务生效了。



Spring 在xml配置里配置事务的更多相关文章
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 不在JPA 的 persistence.xml 文件里配置Entity class的解决的方法
在Spring 集成 Hibernate 的JPA方式中,须要在persistence配置文件里定义每个实体类.这样很地不方便.2种方法能够解决此问题: 这2种方式都能够实现不用在persist ...
- 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- duilib中加入自己定义控件之后怎么可以在xml文件里配置使用
加入自己定义控件可能有两种不同的情况: 1. 在duilib库中加入的自己定义控件. 2. 在我们的应用程序中自己重写了一个控件. 以下開始解说不同的情况下怎么才干支持在xml文件配置控件: 1. ...
- spring基于XML的声明式事务控制
<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...
- 曹工说Spring Boot源码(15)-- Spring从xml文件里到底得到了什么(context:load-time-weaver 完整解析)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Spring在xml配置里配置事务
事先准备:配置数据源对象用<bean>实例化各个业务对象. 1.配置事务管理器. <bean id="transactionManager" class=&quo ...
- 阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤
环境搭建 新建工程 把对应的依赖复制过来 src下内容复制 配置spring中的声明事物 找到bean.xml开始配置 配置事物管理器 里面需要注入DataSource 2-配置事物通知 需要先导入事 ...
随机推荐
- Docker 网络之端口绑定
外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来指定端口映射. -P 标记时 Docker 会随机映射一个 49000~49900 的端口到内部容 ...
- 吴超老师课程--Hbase Shell
hbase提供了一个shell的终端给用户交互 名称 命令表达式 创建表 create '表名称', '列族名称1','列族名称2','列族名称N' 添加记录 put '表名称', '行名称', '列 ...
- java队列的实现
队列也可以通过数组和链表两种方式来实现. 1.链表方式实现 class Node{ Node next = null; int data; public Node(int data){this.dat ...
- [笔记]Go语言写文件几种方式性能对比
Go语言中写文件有多种方式,这里进行如下几种方式的速度对比: 打开文件,写入内容,关闭文件.如此重复多次 打开文件,写入内容,defer 关闭文件.如此重复多次 打开文件,重复多次写入内容,defer ...
- Git 快速入门--Git 基础
Git 快速入门 Git 基础 那么,简单地说,Git 究竟是怎样的一个系统呢? 请注意接下来的内容非常重要,若你理解了 Git 的思想和基本工作原理,用起来就会知其所以然,游刃有余. 在开始学习 G ...
- BZOJ 一句话题解
菜鸡刷题记录 [题号:题解] 1008:简单排列组合 #include <bits/stdc++.h> using namespace std; #define ll long long ...
- rest字符串匹配模式-初次实现方案
一般的rest访问的路径如同这样的路径 http://localhost:8080/AppName/{class}/{method}/{param1}/{param2}... rest的方法分:POS ...
- SpringBoot RedisMQ消息队列与发布订阅
SpringBoot简单整合RedisMQ消息队列和发布订阅 注:RedisMq消息队列使用redis数组实现,leftpush存一,rightpop取一. 1.application.propert ...
- less预编译语言使用总结
以前就使用过less和sass,其实很简单,就是很长时间不用,忘记语法了,现在来总结一片使用技巧 一.注释 less的注释不会被编译到css文件中,所以提倡多使用less中的注释:/**/ 二.变量 ...
- 20145201《Java程序设计》第8周学习总结
20145201 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 15.1.1 日志API简介 java.util.logging包提供了日志功 ...