首页 > 编程语言 > 详细

Spring事务注解(掌握)

时间:2015-10-14 09:10:01      阅读:346      评论:0      收藏:0      [点我收藏+]

注解的XML配置

注解其他配置正常,只是在Spring的XML配置中修改事务这一块
            使用注解弊端 : 添写虽然容易方便,但是代码多了以后修改起来很是麻烦.不利于维护
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  12. <!-- bean definitions here -->
  13. <!-- 1.引入配置文件 -->
  14. <context:property-placeholder location="classpath:jdbc.properties"/>
  15. <!-- 2.配置C3P0连接池 -->
  16. <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  17. <property name="driverClass" value="${jdbc.driverClass}"></property>
  18. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  19. <property name="user" value="${jdbc.user}"></property>
  20. <property name="password" value="${jdbc.password}"></property>
  21. </bean>
  22. <!-- 3.配置事务管理器 (吧连接池放入事务管理器中)-->
  23. <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  24. <property name="dataSource" ref="comboPooledDataSource"></property>
  25. </bean>
  26. <!-- 4.开启注解事务 -->
  27. <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
  28. <!-- 5.配置DAO层实例化对象,并且传入连接池供父类引用 -->
  29. <bean id="accountDao" class="com.mickeymouse.dao.impl.AccountDaoImpl">
  30. <property name="dataSource" ref="comboPooledDataSource"></property>
  31. </bean>
  32. <!-- 6.配置service层 -->
  33. <bean id="accountService" class="com.mickeymouse.service.impl.AccountServiceImpl">
  34. <!-- 属性注入 -->
  35. <property name="dao" ref="accountDao"></property>
  36. </bean>
  37. </beans>

使用方式:

        在业务层上添加一个注解
  1. package com.mickeymouse.service.impl;
  2. import org.springframework.transaction.TransactionStatus;
  3. import org.springframework.transaction.annotation.Transactional;
  4. import org.springframework.transaction.support.TransactionCallback;
  5. import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  6. import org.springframework.transaction.support.TransactionTemplate;
  7. import com.mickeymouse.dao.AccountDao;
  8. import com.mickeymouse.service.AccountService;
  9. @SuppressWarnings("all")
  10. public class AccountServiceImpl implements AccountService{
  11. private AccountDao dao;
  12. public void setDao(AccountDao dao) {
  13. this.dao = dao;
  14. }
  15. /**
  16. * 转账汇款
  17. */
  18. @Transactional
  19. public void transfer(final String deletename, final String addname, final int money) {
  20. dao.addmoney(addname, money);
  21. //int i = 1/0;//模拟断电
  22. dao.deletemoney(deletename, money);
  23. }
  24. }





Spring事务注解(掌握)

原文:http://my.oschina.net/mickeymouse/blog/516823

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!