使用Spring AOP进行事务操作
两种实现方式:
编程式:结构实现复杂,不易于复用
声明式:结构实现简单,易于复用,加入事务和取消事务操作简单(推荐)
- 基于xml配置文件方式(了解)
- 基于注解方式(推荐)
- 底层使用AOP操作
PS: 事务操作,推荐加入到Service层
实现方式演示,准备工作:
1、导入相关依赖包
2、相关类及文件的准备:
jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=123456
实体类
package com.iweb.mall.entity;
import lombok.Data;
/**
* @author cyf
* @data 2021/5/3
* @desc
*/
@Data
public class Bank {
private Integer id;
private String userName;
private Integer money;
}
dao
package com.iweb.mall.dao;
import com.iweb.mall.entity.Bank;
/**
* @author cyf
* @data 2021/5/3
* @desc
*/
public interface BankDao {
/**
* 修改银行账户
* @param bank
* @return
*/
int update(Bank bank);
}
package com.iweb.mall.dao.Impl;
import com.iweb.mall.dao.BankDao;
import com.iweb.mall.entity.Bank;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
* @author cyf
* @data 2021/5/3
* @desc
*/
@Repository(value = "bankDao")
public class BankDaoImpl implements BankDao {
@Resource
private JdbcTemplate jdbcTemplate;
@Override
public int update(Bank bank) {
String sql = "update bank set money = ? where id = ?";
int ret = this.jdbcTemplate.update(sql,bank.getMoney(),bank.getId());
return ret;
}
}
service
package com.iweb.mall.service;
import com.iweb.mall.entity.Bank;
/**
* @author cyf
* @data 2021/5/4
* @desc
*/
public interface BankService {
/**
* 转账业务
* @param fromAccount 扣款账户
* @param toAccount 收款账户
* @param money 转账金额
*/
void transferMoney(Bank fromAccount, Bank toAccount, Integer money);
}
package com.iweb.mall.service.Impl;
import com.iweb.mall.dao.BankDao;
import com.iweb.mall.entity.Bank;
import com.iweb.mall.service.BankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* @author cyf
* @data 2021/5/4
* @desc
*/
@Service
public class BankServiceImpl implements BankService {
@Resource(name = "bankDao")
private BankDao bankDao;
@Override
@Transactional
public void transferMoney(Bank fromAccount, Bank toAccount, Integer money) {
// 1.张三账户减少钱
fromAccount.setMoney(fromAccount.getMoney() - money);
this.bankDao.update(fromAccount);
// 2.李四账户增加钱
toAccount.setMoney(toAccount.getMoney() + money);
this.bankDao.update(toAccount);
// 模拟异常
// int i = 10 / 0;
System.out.println("转账成功");
}
}
测试代码:
package com.iweb.mall.service.Impl;
import com.iweb.mall.entity.Bank;
import com.iweb.mall.service.BankService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
/**
* @author cyf
* @data 2021/5/4
* @desc
*/
public class BankServiceImplTest {
@Test
public void testRemove() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext12.xml");
BankService bankService = context.getBean("bankServiceImpl",BankService.class);
try {
bankService.remove(4);
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void transferMoney() {
try {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext12.xml");
//转账方-张三
Bank bankZhang = new Bank();
bankZhang.setMoney(1000);
bankZhang.setUserName("张三");
bankZhang.setId(1);
//收款方-李四
Bank bankLi = new Bank();
bankLi.setMoney(1000);
bankLi.setUserName("李四");
bankLi.setId(2);
//进行转账操作
BankService bankService = context.getBean("bankServiceImpl",BankService.class);
bankService.transferMoney(bankZhang,bankLi,100);
}catch (Exception e){
e.printStackTrace();
}
}
}
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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 演示基于配置xml方式实现事务操作-->
<!--开启组件扫描,扫描bean-->
<context:component-scan base-package="com.iweb"></context:component-scan>
<!--开启aop 注解扫描-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 导入外部配置文件-->
<context:property-placeholder location="classpath:database1.properties"/>
<!-- 配置druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入datasource数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 2. 配置增强(通知) -->
<tx:advice id="txAdvice">
<!-- 配置事务参数-->
<tx:attributes>
<!-- 指定在哪类方法上添加什么事务-->
<tx:method name="transfer*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置切入点和切面-->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.iweb.mall.service.BankService.*(..))"/>
<!-- 配置切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
测试结果: