本篇文章給大家分享的是有關(guān)如何在spring aop中實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、南開網(wǎng)絡(luò)推廣、成都微信小程序、南開網(wǎng)絡(luò)營(yíng)銷、南開企業(yè)策劃、南開品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供南開建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用的
MethodInterceptor
定義攔截器鏈,MethodInvocation
遞歸進(jìn)入下一個(gè)攔截器鏈中。類圖如下:
MethodInterceptor
public interface MethodInterceptor { Object invoke(MethodInvocation invocation) throws Throwable; }
MethodInvocation
public interface MethodInvocation { Object proceed() throws Throwable; }
AbstractAspectJAdvice
抽象類,實(shí)現(xiàn)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
實(shí)現(xiàn)MethodInvocation
,proceed()
方法遞歸實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用。
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
測(cè)試類,測(cè)試通知
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
時(shí)序圖 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
時(shí)序圖 afterReturningAdvice->beforeAdvice
以上就是如何在spring aop中實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。