Part 1 精讲设计模式
1.7 代理模式
1.7.2 纯手写Spring事务处理
田超凡
2019-10-29
转载请注明原作者
1.基于原生JDK动态代理和自定义事务处理器进行事务处理
代理目标:OrderService接口和OrderServiceImpl接口实现
package com.tcf.design.proxy.prototype.aop.target.service;
/***
* TODO TCF 订单业务接口,代理目标
* @author Hasee
*
*/
public interface OrderService {
//TODO TCF 下单,代理目标方法
public void order();
}
package com.tcf.design.proxy.prototype.aop.target.service.impl;
import org.springframework.stereotype.Service;
import com.tcf.design.proxy.prototype.aop.target.service.OrderService;
/***
* TODO TCF 订单业务接口实现类
* @author Hasee
*
*/
@Service("orderServiceImpl")
public class OrderServiceImpl implements OrderService {
@Override
public void order()
{
System.out.println("代理目标方法执行......");
}
}
JDK动态代理处理器 MyInvocationHandle
package com.tcf.design.proxy.prototype.aop.core;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.transaction.TransactionStatus;
import com.tcf.design.proxy.prototype.aop.util.SpringUtils;
/***
* TODO TCF JDK动态代理核心处理器-InvocationHandle
* @author Hasee
*
*/
public class MyInvocationHandle implements InvocationHandler {
//TODO TCF 代理目标实例
private Object target;
//TODO TCF 自定义事务管理器
private MyTransactionManager transactionManager;
//TODO TCF 构造柱入
public MyInvocationHandle(Object target)
{
this.target=target;
this.transactionManager=SpringUtils.getBean("myTransactionManager",MyTransactionManager.class);
}
//TODO TCF 代理实例需要执行的代理方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Object invokeResult=null;
TransactionStatus status=null;
try
{
//TODO TCF 开启事务处理
status=transactionManager.begin();
//TODO TCF 调用代理目标方法
invokeResult=method.invoke(target,args);
//TODO TCF 事务提交
transactionManager.commit(status);
}
catch(Exception e)
{
e.printStackTrace();
//TODO TCF 事务回滚
if(status!=null)
{
transactionManager.rollBack(status);
}
}
return invokeResult;
}
//TODO TCF 创建JDK动态代理实例
@SuppressWarnings("unchecked")
public <T> T getProxy()
{
return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
this);
}
}
自定义事务管理器
package com.tcf.design.proxy.prototype.aop.core;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
/***
* TODO TCF 事务管理器,封装事务处理操作
* @author Hasee
*
*/
@Component
public class MyTransactionManager {
//TODO TCF Spring AOP事务管理器
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
//TODO TCF 开启事务
public TransactionStatus begin()
{
TransactionStatus status=null;
if(dataSourceTransactionManager!=null)
{
status=dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
}
return status;
}
//TODO TCF 事务提交
public void commit(TransactionStatus status)
{
if(dataSourceTransactionManager!=null)
{
dataSourceTransactionManager.commit(status);
}
}
//TODO TCF 事务回滚
public void rollBack(TransactionStatus status)
{
if(dataSourceTransactionManager!=null)
{
dataSourceTransactionManager.rollback(status);
}
}
}
2.基于Spring AOP AspectJ注解实现事务处理
AOP切面类
package com.tcf.design.proxy.prototype.aop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import com.tcf.design.proxy.prototype.aop.core.MyTransactionManager;
/***
* TODO TCF 事务管理器-切面
* @author Hasee
*
*/
@Aspect
@Component
public class TransactionAspect {
//TODO TCF 事务管理器
@Autowired
private MyTransactionManager transactionManager;
//TODO TCF 切入点
@Pointcut("execution(* com.tcf.design.proxy.prototype.aop.target.service..*.order(..))")
public void order()
{
}
//TODO TCF 异常抛出增强,抛出异常回滚事务
@AfterThrowing(pointcut="order()")
public void afterThrowing()
{
//TODO TCF 当前事务回滚
System.out.println("当前事务回滚......");
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
//TODO TCF 环绕增强,目标方法全部加入事务处理
@Around("execution(* com.tcf.design.proxy.prototype.aop.target.service..*.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
//TODO TCF 开启事务
TransactionStatus status=transactionManager.begin();
//TODO TCF 目标方法执行
proceedingJoinPoint.proceed();
//TODO TCF 事务提交
transactionManager.commit(status);
}
}
转载请注明原作者