如何在springaop中实现链式调用
本篇文章给大家分享的是有关如何在spring aop中实现链式调用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站设计、南开网络推广、成都微信小程序、南开网络营销、南开企业策划、南开品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供南开建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
实现链式调用的
MethodInterceptor
定义拦截器链,MethodInvocation
递归进入下一个拦截器链中。类图如下:
MethodInterceptor
public interface MethodInterceptor { Object invoke(MethodInvocation invocation) throws Throwable; }
MethodInvocation
public interface MethodInvocation { Object proceed() throws Throwable; }
AbstractAspectJAdvice
抽象类,实现MethodInterceptor
public abstract class AbstractAspectJAdvice implements MethodInterceptor{ private Method adviceMethod; private Object adviceObject; public AbstractAspectJAdvice(Method adviceMethod, Object adviceObject) { this.adviceMethod = adviceMethod; this.adviceObject = adviceObject; } public Method getAdviceMethod() { return this.adviceMethod; } public void invokeAdviceMethod() throws Throwable { adviceMethod.invoke(adviceObject); } }
AspectJBeforeAdvice
前置通知
public class AspectJBeforeAdvice extends AbstractAspectJAdvice { public AspectJBeforeAdvice(Method method, Object adviceObject) { super(method, adviceObject); } @Override public Object invoke(MethodInvocation invocation) throws Throwable{ this.invokeAdviceMethod(); Object o = invocation.proceed(); return o; } }
AspectJAfterReturningAdvice
后置通知
public class AspectJAfterReturningAdvice extends AbstractAspectJAdvice { public AspectJAfterReturningAdvice(Method method, Object adviceObject) { super(method, adviceObject); } @Override public Object invoke(MethodInvocation invocation) throws Throwable{ Object o = invocation.proceed(); this.invokeAdviceMethod(); return o; } }
ReflectiveMethodInvocation
实现MethodInvocation
,proceed()
方法递归实现链式调用。
public class ReflectiveMethodInvocation implements MethodInvocation { private final Object targetObject; private final Method targetMethod; private final ListinterceptorList; private int currentInterceptorIndex = -1; public ReflectiveMethodInvocation(Object targetObject, Method targetMethod, List interceptorList) { this.targetObject = targetObject; this.targetMethod = targetMethod; this.interceptorList = interceptorList; } @Override public Object proceed() throws Throwable { if (this.currentInterceptorIndex == this.interceptorList.size() - 1) { return invokeJoinPoint(); } this.currentInterceptorIndex++; MethodInterceptor interceptor = this.interceptorList.get(this.currentInterceptorIndex); return interceptor.invoke(this); } private Object invokeJoinPoint() throws Throwable { return this.targetMethod.invoke(this.targetObject); } }
NioCoderService
模拟service
类
public class NioCoderService { public void testAop() { System.out.println("http://niocoder.com/"); } }
TransactionManager
模拟通知类
public class TransactionManager { public void start() { System.out.println("start tx"); } public void commit() { System.out.println("commit tx"); } public void rollback() { System.out.println("rollback tx"); } }
ReflectiveMethodInvocationTest
beforeAdvice->afterReturningAdvice
测试类,测试通知
public class ReflectiveMethodInvocationTest { private AspectJBeforeAdvice beforeAdvice = null; private AspectJAfterReturningAdvice afterReturningAdvice = null; private NioCoderService nioCoderService; private TransactionManager tx; public void setUp() throws Exception { nioCoderService = new NioCoderService(); tx = new TransactionManager(); beforeAdvice = new AspectJBeforeAdvice(TransactionManager.class.getMethod("start"), tx); afterReturningAdvice = new AspectJAfterReturningAdvice(TransactionManager.class.getMethod("commit"), tx); } public void testMethodInvocation() throws Throwable { Method method = NioCoderService.class.getMethod("testAop"); ListinterceptorList = new ArrayList<>(); interceptorList.add(beforeAdvice); interceptorList.add(afterReturningAdvice); ReflectiveMethodInvocation mi = new ReflectiveMethodInvocation(nioCoderService, method, interceptorList); mi.proceed(); } public static void main(String[] args) throws Throwable { ReflectiveMethodInvocationTest reflectiveMethodInvocationTest = new ReflectiveMethodInvocationTest(); reflectiveMethodInvocationTest.setUp(); reflectiveMethodInvocationTest.testMethodInvocation(); } }
输出:
start tx
http://niocoder.com/
commit tx
时序图 beforeAdvice->afterReturningAdvice
afterReturningAdvice->beforeAdvice
修改interceptorList
的顺序
public void testMethodInvocation() throws Throwable { Method method = NioCoderService.class.getMethod("testAop"); ListinterceptorList = new ArrayList<>(); interceptorList.add(afterReturningAdvice); interceptorList.add(beforeAdvice); ReflectiveMethodInvocation mi = new ReflectiveMethodInvocation(nioCoderService, method, interceptorList); mi.proceed(); }
输出:
start tx
http://niocoder.com/
commit tx
时序图 afterReturningAdvice->beforeAdvice
以上就是如何在spring aop中实现链式调用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
当前题目:如何在springaop中实现链式调用
URL标题:http://azwzsj.com/article/ggsedo.html