真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

責(zé)任鏈模式在SpringAOP中怎么用

小編給大家分享一下責(zé)任鏈模式在SpringAOP中怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)提供高防物理服務(wù)器租用、云服務(wù)器、香港服務(wù)器、四川服務(wù)器托管

當(dāng)一個對象在一條鏈上被多個攔截器攔截處理時,我們這樣的設(shè)計模式稱為責(zé)任鏈模式,它用于一個對象在多個角色中傳遞的場景。

SpringAOP就是利用動態(tài)代理和責(zé)任鏈模式實現(xiàn)的,當(dāng)一個切面有多個織入時,這些需要織入的方法就形成了一個責(zé)任鏈,就像Filter鏈一樣。

下面就模擬一下springaop中的責(zé)任鏈:

接口:

public interface Joinpoint {

Object proceed() throws Throwable;

}

public interface MethodInvocation extends Joinpoint {

}

定義攔截器接口

public interface MethodInterceptor {

Object invoke(MethodInvocation mi) throws Throwable;

}

定義前置通知,在目標(biāo)方便調(diào)用前執(zhí)行通知:

public class MethodBeforeAdviceInterceptor implements MethodInterceptor{

@Override

public Object invoke(MethodInvocation mi) throws Throwable {

System.out.println("I am BeforeAdvice");

return mi.proceed();

}

}

定義后置通知,在目標(biāo)方法完成后執(zhí)行通知:

public class AspectJAfterAdvice implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation mi) throws Throwable {

Object var;

try {

var = mi.proceed();

}finally {

System.out.println("I am AfterAdvice");

}

return var;

}

}

中間類,攔截器鏈調(diào)用邏輯:

public class ReflectiveMethodInvocation implements MethodInvocation{

List methodInterceptors;

public ReflectiveMethodInvocation(List methodInterceptors) {

this.methodInterceptors = methodInterceptors;

}

private int index = -1;

@Override

public Object proceed() throws Throwable {

Object var = null;

if (index == this.methodInterceptors.size()-1) {

System.out.println("真正的目標(biāo)方法");

return new String("ha");

}else{

var = methodInterceptors.get(++index).invoke(this);

}

return var;

}

}

測試類:

public class Test {

public static void main(String[] args) throws Throwable {

AspectJAfterAdvice aspectJAfterAdvice = new AspectJAfterAdvice();

MethodBeforeAdviceInterceptor methodBeforeAdviceInterceptor = new MethodBeforeAdviceInterceptor();

List methodInterceptors = new ArrayList<>();

methodInterceptors.add(methodBeforeAdviceInterceptor);

methodInterceptors.add(aspectJAfterAdvice);

ReflectiveMethodInvocation reflectiveMethodInvocation = new ReflectiveMethodInvocation(methodInterceptors);

reflectiveMethodInvocation.proceed();

}鄭州好的婦科醫(yī)院 http://www.zzkedayy.com/

}

執(zhí)行結(jié)果:

I am BeforeAdvice

真正的目標(biāo)方法

I am AfterAdvice

下面是springAOP中的源碼:

首先看JdkDynamicAopProxy類中的invoke方法:

final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

Object oldProxy = null;

boolean setProxyContext = false;

TargetSource targetSource = this.advised.targetSource;

Class targetClass = null;

Object target = null;

Integer var10;

try {

if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {

Boolean var20 = this.equals(args[0]);

return var20;

}

if (this.hashCodeDefined || !AopUtils.isHashCodeMethod(method)) {

if (method.getDeclaringClass() == DecoratingProxy.class) {

Class var18 = AopProxyUtils.ultimateTargetClass(this.advised);

return var18;

}

Object retVal;

if (!this.advised.opaque && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) {

retVal = AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);

return retVal;

}

if (this.advised.exposeProxy) {

oldProxy = AopContext.setCurrentProxy(proxy);

setProxyContext = true;

}

target = targetSource.getTarget();

if (target != null) {

targetClass = target.getClass();

}

以上是“責(zé)任鏈模式在SpringAOP中怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


文章題目:責(zé)任鏈模式在SpringAOP中怎么用
瀏覽地址:http://weahome.cn/article/piosis.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部