AccountServiceImpl
public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public AccountDao getAccountDao() {return accountDao;}//在AccountServiceimpl中注入AccountDao,要提供set方法//配置中有<property name="accountDao" ref="accountDao"></property>public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void account(String outName, String inName, double money) {// TODO Auto-generated method stubaccountDao.accountOut(outName, money);//设置异常int i = 10/0;accountDao.accountIn(inName, money);}}
AccountDao
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {//AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate
//所以这里选择用this.getJdbcTemplate来获取JdbcTemplate@Overridepublic void accountIn(String inName, double money) {// TODO Auto-generated method stubthis.getJdbcTemplate().update("update account set money = money+? where name = ?",money,inName);}@Overridepublic void accountOut(String outName, double money) {// TODO Auto-generated method stubthis.getJdbcTemplate().update("update account set money = money-? where name = ?",money,outName);}}
Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AccountTest {@Autowired//(将applicationContext中的accountService注入)private AccountService accountService;@Testpublic void test1() {accountService.account("tom", "fox", 500);}}
applicationContext.xml
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///springtest"/><property name="user" value="root"/><property name="password" value="root"></property></bean><!--(配置AccountServiceImpl)  --><bean id="accountService" class="service.AccountServiceImpl"><!-- 注入dao,此时需要在AccountServiceImplements中为accountDao创建set方法 --><property name="accountDao" ref="accountDao"></property></bean><!-- 配置AccountDaoImpl --><bean id="accountDao" class="dao.AccountDaoImpl"><!-- 注入dataSource,由于AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate --><property name="dataSource" ref="c3p0dataSource"></property></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0dataSource"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必须的,对哪些方法进行事务控制isolation 可选 设置事务隔离级别 默认是DEFAULT propagation:可选 设置事务传播 默认值 REQUIREDtimeout 可选 超时时间 默认值-1 read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚--><tx:method name="account"/></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* service.AccountService.account(..))" id="txPointCut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/></aop:config><!-- 配置采用注解方式 --><tx:annotation-driven transaction-manager="transactionManager"/>   <!-- 注入dataSource,由于AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate,这里便不用再配置 --><!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="c3p0dataSource"/></bean> -->

问题:关于xml方式与annotation方式进行事务管理的优缺点?
1.从简单上来说,使用注解更方便。
2.使用配置的方案,可以对事务配置进行集中维护:

<tx:method name="get*" />
<tx:method name="save*" />

Spring事务管理案例相关推荐

  1. Spring事务管理的demo

    事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败,最为典型的就是银行转账的案例: A要向B转账,现在A,B各自账户中有1000元,A要给B转200元,那么这个转账就必须保证是一个事务,防止中 ...

  2. Spring 事务管理的详细讲解及使用

    文章目录 一. Spring 事务管理简介 二.Spring 事务管理器 1.Spring 事务管理接口 1.1. PlatformTransactionManager 接口 1.2. Transac ...

  3. Spring事务管理 与 SpringAOP

    1,Spring事务的核心接口 Spring事务管理的实现有许多细节,如果对整个接口框架有个大体了解会非常有利于我们理解事务,下面通过讲解Spring的事务接口来了解Spring实现事务的具体策略.  ...

  4. 什么是事务的传播_这么漂亮的Spring事务管理详解,你不来看看?

    事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用 ...

  5. SPRING 事务管理说明

    spring 事务管理是通过AOP拦截指定的方法,进行事务管理. 事务配置 <aop:config proxy-target-class="true"><aop: ...

  6. Spring 事务管理高级应用难点剖析

    Spring 事务管理高级应用难点剖析: 第 1 部分 http://www.ibm.com/developerworks/cn/java/j-lo-spring-ts1/index.html htt ...

  7. spring事务管理 TransactionProxyFactoryBean源码分析

    J2EE,当然离不开事务,事务又当然少不了Spring声明式事务.spring声明式事务,很多码农门,应该和笔者一样,停留在使用上,及仅仅了解点原理.如:Spring事务管理原理"代理+AO ...

  8. Spring 事务管理高级应用难点剖析--转

    第 1 部分 http://www.ibm.com/search/csass/search/?q=%E4%BA%8B%E5%8A%A1&sn=dw&lang=zh&cc=CN& ...

  9. Spring事务管理(详解+实例)

    写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: ...

最新文章

  1. Flask之请求钩子
  2. 算法--------------整数反转
  3. oa php mysql_PHP+MYSQL的OA为何没有Java的值钱?
  4. [转]div里table居中的问题 Div与body顶部间隙
  5. js轮播图片小圆点变化_原生js实现轮播图(两种方法)
  6. 回顾 | 进击吧! Blazor !第三期 信息交互
  7. 不用第三方插件如何统计自己wordpress的访问量
  8. c语言第4份实验报告,C语言实验报告04.doc
  9. Python面试必须要看的15个问题
  10. 计算机术语中英文对照表(流水线/微架构/体系结构/指令集)
  11. 一图总结:软件测试原则|策略|模型|生命周期
  12. fianl属性 java_Java反射如何有效的修改final属性值详解
  13. Linux 下Nginx+Tomcat 完美整合 nginx tomcat 整合方式
  14. IntelliJ IDEA如何去掉xml文件背景色
  15. 工科数学分析之数学感悟
  16. Oracle如何创建数据库
  17. 帮你举例说明什么是Python鸭子类型
  18. IDEA怎样自定义 Touch Bar
  19. 必知之vs2019添加外加库文件操作
  20. 图片轮播器——javascript

热门文章

  1. 从“零”开发一款知识图谱应用产品
  2. Dns与httpDNS的区别
  3. Uaexpert操作手册
  4. 【数据结构】广义表的基本概念
  5. list.isEmpty() CollectionUtils.isEmpty(list)区别?
  6. atl常量暴露的最简便方法
  7. Arduino设置esp8266实现局域网通信
  8. HtmlUnit入门教程
  9. Hi3516开发笔记(三):Hi3516虚拟机基础环境搭建之交叉编译环境境搭建以及开机启动脚本分析
  10. 基于Eclipse和Mysql写的公交管理系统