您好,登錄后才能下訂單哦!
小編給大家分享一下責任鏈模式在SpringAOP中怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
當一個對象在一條鏈上被多個攔截器攔截處理時,我們這樣的設計模式稱為責任鏈模式,它用于一個對象在多個角色中傳遞的場景。
SpringAOP就是利用動態(tài)代理和責任鏈模式實現(xiàn)的,當一個切面有多個織入時,這些需要織入的方法就形成了一個責任鏈,就像Filter鏈一樣。
下面就模擬一下springaop中的責任鏈:
接口:
public interface Joinpoint {
Object proceed() throws Throwable;
}
public interface MethodInvocation extends Joinpoint {
}
定義攔截器接口
public interface MethodInterceptor {
Object invoke(MethodInvocation mi) throws Throwable;
}
定義前置通知,在目標方便調(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();
}
}
定義后置通知,在目標方法完成后執(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("真正的目標方法");
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
真正的目標方法
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();
}
以上是“責任鏈模式在SpringAOP中怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。