一、创建spring项目
    项目名称:spring101311
二、在项目上添加jar包
    1.在项目中创建lib目录
        /lib
    2.在lib目录下添加spring支持
        com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
        com.springsource.org.aopalliance-1.0.0.jar
        com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        mysql-connector-java-5.1.18-bin.jar
        spring-aop-3.2.0.RELEASE.jar
        spring-aspects-3.2.0.RELEASE.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
        spring-jdbc-3.2.0.RELEASE.jar
        spring-tx-3.2.0.RELEASE.jar
三、在项目中添加配置文件与属性文件
    1.在项目中创建conf目录
    2.在conf目录下添加属性文件
        属性文件名称:jdbc.properties
        属性文件内容:
        jdbc.url=jdbc:mysql://localhost:3306/spring
        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.username=root
        jdbc.password=root
    2.在conf目录下添加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"
                       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">
                
                <!-- 1.加载属性文件 -->
                <context:property-placeholder location="classpath:jdbc.properties"/>
                
                <!-- 2.配置数据库连接池 -->
                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <property name="jdbcUrl" value="${jdbc.url}"></property>
                    <property name="driverClass" value="${jdbc.driver}"></property>
                    <property name="user" value="${jdbc.username}"></property>
                    <property name="password" value="${jdbc.password}"></property>
                </bean>
        </beans>
四、实现bean设计
    1.在src目录下创建实体bean的包
        包名:cn.jbit.spring101310.domain
    2.在包下创建实体bean
        public class Account {
            private Integer id;
            private String name;
            private Double money;
            //省略get and set
        }
五、设计Dao层
    1.在src目录下创建dao层的包
        包名:cn.jbit.spring101310.dao
    2.在包下创建dao层的接口与实现类
        1)接口设计
            接口名称:IAccountDao.java
            接口内容:
            public interface IAccountDao {
                /**
                 * 转出
                 */
                public void outMoney(Account outaccount);
                /**
                 * 转入
                 */
                public void inMoney(Account inaccount);
            }
        2)接口实现类设计
            实现类名称:AccountDaoImpl.java
            实现类内容:
            public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
                /**
                 * 转入
                 */
                @Override
                public void inMoney(Account inaccount) {
                    String sql = "update account set money = money + ? where id = ?";
                    this.getJdbcTemplate().update(sql,inaccount.getMoney(),inaccount.getId());
                }
            
                /**
                 * 转出
                 */
                @Override
                public void outMoney(Account outaccount) {
                    String sql = "update account set money = money - ? where id = ?";
                    this.getJdbcTemplate().update(sql,outaccount.getMoney(),outaccount.getId());        
                }
            }
六、在核心配置文件中配置Dao
    <!-- 3.配置Dao -->
    <bean id="accountDao" class="cn.jbit.spring101310.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

七、在核心配置文件中配置事务相关信息
    <!-- 4.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
八、业务层设计
    1.在src目录下创建业务层的包
        包名:cn.jbit.spring101310.service
    2.在包下创建业务层的接口与实现类
        1)接口设计
            接口名称:AccountService.java
            接口内容:
            public interface AccountService {
                public void transfer(Account outAccount,Account inAccount);
            }
        2)接口实现类设计
            实现类名称:AccountServiceImpl.java
            实现类内容:
            public class AccountServiceImpl implements AccountService {
            
                private IAccountDao accountDao;
                @Override
                public void transfer(final Account outAccount, final Account inAccount) {
                    //转出
                    accountDao.outMoney(outAccount);
                    int a = 1/0;
                    //转入
                    accountDao.inMoney(inAccount);
                }
                //省略get and set
            }
九、在核心配置文件中配置业务层
    <!-- 5.配置Service -->
    <bean id="accountService" class="cn.jbit.spring101310.service.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
十、在核心配置文件中配置代理
    <!-- 6.创建代理 -->
    <bean id="accountServiceproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!--
            引用事务管理器
         -->
        <property name="transactionManager" ref="transactionManager"></property>
        <!--
            为哪个类创建代理
         -->
        <property name="target" ref="accountService"></property>
        <property name="transactionAttributes">
            <props>
                <!--
                    *:所有方法
                    PROPAGATION_REQUIRED:默认的事务传播行为
                 -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
十一、测试
    1.在项目上创建test目录
        /test
    2.在test目录下创建测试包
        包名:cn.jbit.spring101310.service
    3.在测试包下创建测试类
        测试类名:AccountServiceTest.java
        测试类的内容:
        public class AccountServiceTest {
            @Test
            public void testTranser(){
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                AccountService accountService = (AccountService) context.getBean("accountService");
                Account outAccount = new Account();
                outAccount.setId(1);
                outAccount.setMoney(500D);
                Account inAccount = new Account();
                inAccount.setId(2);
                inAccount.setMoney(500D);
                accountService.transfer(outAccount, inAccount);
            }
        }

转载于:https://blog.51cto.com/suyanzhu/1563387

spring-声明式事务管理相关推荐

  1. Spring声明式事务管理、事务的传播行为xml配置

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. <tx:method name="insert*" propagat ...

  2. Spring配置文件详解三:Spring声明式事务管理

    1.声明式事务管理 Spring提供了声明式事务管理,这是通过Spring AOP实现的. 原理:Spring中进行事务管理的通常方式是利用AOP(面向切片编程)的方式,为普通java类封装事务控制, ...

  3. Spring声明式事务管理

    事务管理方式 1.编码方案 不建议使用,它具有侵入性.在原有的业务代码基础上去添加事务管理代码 2. 声明式事务控制,基于AOP对目标进行代理,添加around环绕通知. 这种方案,它不具有侵入性,不 ...

  4. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  5. 【Spring学习笔记 九】Spring声明式事务管理实现机制

    什么是事务?事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用,关乎数据准确性的地方我们一定要用到事务,防止业务逻辑出错. 什么是事务管理,事务管理对于企业应用而言至 ...

  6. Spring声明式事务管理的配置详解

    环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...

  7. Spring声明式事务管理实现及原理详解

    目录 1.实现步骤 1.1.配置事务管理器 1.2.启动事务注解 1.3.业务添加注解 2.代码演示 2.1.bean文件 2.2.目标类 2.3.测试类 3.Spring事务属性 3.1.传播行为 ...

  8. Spring声明式事务管理中的事务回滚

    一:使用 本文在spring + spring mvc + mybatis中使用 第一步配置xml:注意xml最前面tx名称空间一定要配置 <beans xmlns="http://w ...

  9. 全面分析 Spring 的编程式事务管理及声明式事务管理(转)

    摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...

  10. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

最新文章

  1. Android使用ksoap2-android调用WebService学习
  2. 全排列(含递归和非递归的解法)
  3. linux下Bash编程until语句及格式化硬盘分区等编写脚本(十)
  4. 马斯克终于承认售出比特币:卖了10%
  5. linux 免密登录
  6. SQL Server 2005系列教学(12) 导入导出服务
  7. Axure RP9Team版可用授权
  8. C语言之根据摄氏温度求华氏温度
  9. mac使用mysql出现的错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  10. 王者荣耀静态页面头部代码设计(2)
  11. 古代银票里的加密知识
  12. 设计师思维 工程师思维_从设计思维到设计行为
  13. C语言输出汉字版乘法口诀表,C语言实现打印乘法口诀表
  14. 计算机竞赛一等奖学校名单,信息竞赛获奖名单出炉!这些学校榜上有名
  15. PHP中preg_match详解
  16. 写一副对子_一副对子的传奇故事
  17. java的环境变量如何设置
  18. python的StringIO
  19. 计算机电子技术叠加原理,叠加定理适用于什么电路?电路叠加原理例题解析
  20. 数据结构实践——B-树的基本操作

热门文章

  1. Go之十大经典排序算法
  2. spark yarn任务的executor 无故 timeout之原因分析
  3. 移动測试技术保护源码!解码全球首款移动端白盒測试工具ThreadingTest (文章转自己主动点科技)...
  4. 印象笔记设计经理王怀千:全栈设计师的职业本质
  5. Apple Pay与银联的联姻,是战略,不是产品
  6. 【pmcaff】玩智能硬件的小伙伴,这些你用过么!
  7. 这份程序员的简历刷爆了九月的朋友圈
  8. Spring Cloud和Dubbo
  9. 中国经验对印度等金砖国家智慧城市建设的启示
  10. Jenkins全新的UI体验-Blue Ocean