Spring声明式事务项目实例

使用spring的声明式事务管理来搭建一个转账项目。

事务的介绍:事务是一系列动作的组合操作,这个组合里面的所有动作要么都完成要么都不完成。
事务的特性(ASID):
1.原子性:事务中的所有操作要么都完成要么都不完成(如果有一个动作失败会进行回滚操作)。
2.一致性:事务开始和结束之间的中间状态不会被其他事务看到。
3.隔离性:多个事务同时运行时不会相互影响。
4.持久性:一旦事务结束(无论成功与否),不管系统遇到什么错误都不会改变结果。

1、导入相关jar包


image.png

2、编写spring的配置文件

<?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: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/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
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 

<!-- 配置开启注解 -->
<context:component-scan base-package="com.lj"/>

<!-- 配置开启aop -->
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>

<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8"/>
    <property name="user" value="root"/>
    <property name="password" value="1234"/>
</bean>

<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

3、编写实现类
dao层:

package com.lj.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class AccountDao {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void moreMoney(int money,String name){
        String sql = "update account set money=money+? where name=?";
        jdbcTemplate.update(sql,money,name);
    }
    
    public void lessMoney(int money,String name){
        String sql = "update account set money=money-? where name=?";
        jdbcTemplate.update(sql,money,name);
    }
    
}

service层

package com.lj.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Transactional
@Component
public class AccountService {
    @Autowired
    private AccountDao accountDao;
    
    public void transfer(String man1,String man2,int money){
        accountDao.lessMoney(money, man1);
        accountDao.moreMoney(money, man2);
    }
}

可以使用spring的aop编写一个转账日志记录类

package com.lj.tx;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAccountService {
    
    @After(value="execution(* com.lj.tx.AccountService.*(..))")
    public void log(JoinPoint jp){
        System.out.println("转账记录"+new Date());
        Object args[] = jp.getArgs();
        System.out.println("转账人:"+args[0]+"  收账人:"+args[1]+"  金额:"+args[2]);
    }
}

测试类:

package com.lj.tx;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    @org.junit.Test
    public void txTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("springTX.xml");
        AccountService service = (AccountService) context.getBean("accountService");
        service.transfer("张三", "李四", 100);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,314评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,032评论 6 342
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,632评论 1 133
  • 很多人喜欢这篇文章,特此同步过来 由浅入深谈论spring事务 前言 这篇其实也要归纳到《常识》系列中,但这重点又...
    码农戏码阅读 10,202评论 2 59
  • 原理 现代浏览器在使用CSS3动画时,以下四种情形绘制的效率较高,分别是: 改变位置 改变大小 旋转 改变透明度 ...
    ddai_Q阅读 9,122评论 0 11