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