Spring声明式事务项目实例

2017-06-30  本文已影响0人  li_jun

使用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);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读