在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理。

使用 Annotation 的方式非常简单,只需要在项目中做两件事,具体如下。
1)在 Spring 容器中注册驱动,代码如下所示:
<tx:annotation-driven transaction-manager="txManager"/>
2)在需要使用事务的业务类或者方法中添加注解 @Transactional,并配置 @Transactional 的参数。关于 @Transactional 的参数如图

1. 注册驱动
修改 Spring 配置文件 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:c3p0-db.properties" />
<!-- 配置数据源,读取properties文件信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置dao -->
<bean id="accountDao" class="com.mengma.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- 配置service -->
<bean id="accountService" class="com.mengma.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao" />
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注册事务管理驱动 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
上述代码中可以看出,与原来的配置文件相比,这里只修改了事务管理器部分,新添加并注册了事务管理器的驱动。
需要注意的是,在学习 AOP 注解方式开发时,需要在配置文件中开启注解处理器,指定扫描哪些包下的注解,这里没有开启注解处理器是因为在第 33~35 行手动配置了 AccountServiceImpl,而 @Transactional 注解就配置在该类中,所以会直接生效。
2. 添加 @Transactional 注解
修改 AccountServiceImpl,在文件中添加 @Transactional 注解及参数,添加后如下所示。
package com.mengma.service.impl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.mengma.dao.AccountDao;
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public class AccountServiceImpl {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfer(String outUser, String inUser, int money) {
this.accountDao.out(outUser, money);
// 模拟断电
int i = 1 / 0;
this.accountDao.in(inUser, money);
}
}
需要注意的是,在使用 @Transactional 注解时,参数之间用“,”进行分隔。

最新文章

  1. SSISDB8:使用SSISDB记录的消息Troubleshoot packages
  2. Unity3D优化总结(一)
  3. [Doxygen]Doxygen
  4. Mongodb 服务(windows环境下)因被强制关闭,导致服务不能启动的处理办法
  5. HDU-2159FATE(二维完全背包)
  6. UVA 12532 Interval Product
  7. 性能测试之系统监控工具nmon
  8. [Android开发系列]IT博客应用V1.3
  9. Windows平台下的session0创建进程的问题与解决办法
  10. jQuery显示与隐藏返回顶层的箭头
  11. android 数据存储分配的一些事
  12. [bzoj3203][Sdoi2013]保护出题人
  13. Swift基础之设计折线坐标图
  14. Docker使用docker-compose.yml构建Asp.Net Core和Mysql镜像并与Mysql数据库通信
  15. nodeJs和JavaScript的异同(转)
  16. Web前端3.0时代,“程序猿”如何“渡劫升仙”
  17. MyEclipse项目中,让修改后的Servlet文件立即运行生效方法
  18. [ 9.22 ]CF每日一题系列—— 484A Bits
  19. 如何让Snippet Compiler 2008 支持linq
  20. javascript callee和caller

热门文章

  1. redis队列与RabbitMQ队列区别
  2. 小程序本地存储之wx.getStorageSync
  3. spring事务代码实践
  4. linuxshell命令学习1——用户、文件和目录操作
  5. jdk1.7推出的Fork/Join提高业务代码处理性能
  6. loadrunner11完整卸载
  7. Kubernetes 各版本镜像列表
  8. 解题报告:luogu P2678 跳石头
  9. jquery动态选中radio,获取radio选中值
  10. PG、GP与MySQL的特点和区别