Spring声明式事务管理(基于XML方式实现)
来源:互联网 发布:图像处理 分水岭算法 编辑:程序博客网 时间:2024/05/29 16:55
--------------------siwuxie095
Spring 声明式事务管理(基于 XML 方式实现)
以转账为例
1、基于XML 配置文件的方式实现声明式事务管理,其思想就是AOP 思想
2、导入相关jar 包(共13 个包)
(1)导入核心 jar 包和日志相关的 jar 包
(2)导入JdbcTemplate 的 jar 包
(3)导入MySQL 的 JDBC 驱动包
mysql-connector-java下载链接:
https://dev.mysql.com/downloads/connector/j/
(4)导入 AOP 和AspectJ 的 jar 包
其中:
aopalliance下载链接:
http://mvnrepository.com/artifact/aopalliance/aopalliance
aspectjweaver下载链接:
http://mvnrepository.com/artifact/org.aspectj/aspectjweaver
3、在MySQL 中手动创建数据库和表
数据库名:tx_db,表名:account,字段:id、name、money
手动添加数据,用作测试
4、具体步骤
(1)配置事务管理器
<!--配置事务管理器 -->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--
在 DataSourceTransactionManager源代码中有
属性 dataSource和其 set方法,所以可以注入
-->
<propertyname="dataSource"ref="dataSource"/>
</bean>
(2)配置事务的通知(增强)
<!--配置事务的通知(增强) -->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!--
isolation="DEFAULT"隔离级别
propagation="REQUIRED"传播行为
read-only="false"只读
timeout="-1"过期时间
rollback-for="" -Exception
no-rollback-for="" +Exception
-->
<!--
对进行事务操作的方法(一般是业务层方法)设置匹配规则,
如:transfer*即所有以 transfer 开头的方法
-->
<tx:methodname="transfer"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
(3)配置AOP
<!--配置 AOP -->
<aop:config>
<!--配置切入点 -->
<aop:pointcutexpression="execution(* com.siwuxie095.service.AccountService.transfer(..))"id="pt"/>
<!--配置切面(增强 +切入点) -->
<aop:advisoradvice-ref="txAdvice"pointcut-ref="pt"/>
</aop:config>
5、具体实现
(1)编写一个Dao 类
AccountDao.java:
package com.siwuxie095.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDao {
private JdbcTemplate jdbcTemplate;
publicvoid setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
*转出
*/
publicvoid lessMoney(String from,int money) {
String sql="update account set money=money-? where name=?";
jdbcTemplate.update(sql, money, from);
}
/**
*转入
*/
publicvoid moreMoney(String to,int money) {
String sql="update account set money=money+? where name=?";
jdbcTemplate.update(sql, money, to);
}
}
(2)编写一个Service 类
AccountService.java:
package com.siwuxie095.service;
import com.siwuxie095.dao.AccountDao;
public class AccountService {
private AccountDao accountDao;
publicvoid setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
*转账
*/
publicvoid transfer(String from,String to,int money) {
accountDao.lessMoney(from, money);
//即便中间出现了什么异常,也会进行回滚
//如:int num=10/0;
accountDao.moreMoney(to, money);
}
}
(3)在配置文件中进行配置
applicationContext.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置内置连接池 -->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<!--
jdbc:mysql:///tx_db是 jdbc:mysql://localhost:3306/tx_db的简写
-->
<propertyname="url"value="jdbc:mysql:///tx_db"/>
<propertyname="username"value="root"/>
<propertyname="password"value="8888"/>
</bean>
<!--配置事务管理器 -->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--
在 DataSourceTransactionManager源代码中有
属性 dataSource和其 set方法,所以可以注入
-->
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--配置事务的通知(增强) -->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!--
isolation="DEFAULT"隔离级别
propagation="REQUIRED"传播行为
read-only="false"只读
timeout="-1"过期时间
rollback-for="" -Exception
no-rollback-for="" +Exception
-->
<!--
对进行事务操作的方法(一般是业务层方法)设置匹配规则,
如:transfer*即所有以 transfer 开头的方法
-->
<tx:methodname="transfer"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置 AOP -->
<aop:config>
<!--配置切入点 -->
<aop:pointcutexpression="execution(* com.siwuxie095.service.AccountService.transfer(..))"id="pt"/>
<!--配置切面(增强 +切入点) -->
<aop:advisoradvice-ref="txAdvice"pointcut-ref="pt"/>
</aop:config>
<!--配置对象并注入属性 -->
<beanid="accountService"class="com.siwuxie095.service.AccountService">
<propertyname="accountDao"ref="accountDao"></property>
</bean>
<beanid="accountDao"class="com.siwuxie095.dao.AccountDao">
<propertyname="jdbcTemplate"ref="jdbcTemplate"></property>
</bean>
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<!--
在 JdbcTemplate源代码中有属性 dataSource
和其 set方法,所以可以注入
-->
<propertyname="dataSource"ref="dataSource"></property>
</bean>
</beans>
(4)编写一个测试类
TestDemo.java:
package com.siwuxie095.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.siwuxie095.service.AccountService;
public class TestDmo {
/**
*手动加上 @Test以进行单元测试(将自动导入 JUnit 4的 jar包)
*
*选中方法名,右键->Run As->JUint Test
*/
@Test
publicvoid testService() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService=(AccountService) context.getBean("accountService");
accountService.transfer("小白","小黑",1000);
}
}
【made by siwuxie095】
- Spring声明式事务管理(基于XML方式实现)
- spring声明式事务管理(基于AspectJ的xml方式)
- spring 声明式事务管理xml方式实现
- Spring声明式事务管理(基于注解方式实现)
- Spring声明式事务管理(基于xml的方式)
- Spring 声明式事务管理----基于AspectJ的XML方式
- Spring 声明式事务管理----基于注解方式
- spring声明式事务管理(基于TransactionProxyFactoryBean的方式)
- XML方式实现声明式事务管理
- spring 声明式事务管理注解方式实现
- 深入解读Spring Framework事务管理(第三弹:基于AspectJ的XML声明式事务管理)
- spring声明式事务管理(基于注解)
- Spring声明式事务管理(基于TransactionProxyFactoryBean)
- Spring声明式事务管理(基于AspectJ)
- Spring事务管理(XML配置声明式)
- 使用Spring 2.0 新特性实现声明式事务管理-基于XML Schema
- 使用Spring 2.0 新特性实现声明式事务管理-基于XML Schema
- xml方式实现spring的声明式事务管理及对jdbc操作的支持
- 今日头条|张一鸣:我遇到的优秀年轻人的5个特质
- CentOS安装JDK
- Nginx环境下http和https(ssl)共存的方法
- POJ 2409
- 字符数组全排列组合算法汇总
- Spring声明式事务管理(基于XML方式实现)
- OJ提交题目中的语言选项里G++与C++的区别
- SpringMVC详解(三)------基于注解的入门实例
- VBA-对比方法系列-01-分割内容
- java鬼混笔记:Hibernate:6、二级缓存配置
- jQuery datatable 多个查询条件 ,自定义提交事件
- 2017/8/23训练日记
- Sqlite数据库的创建与方法
- Spring的事务管理机制