責任鏈模式在SpringAOP中的使用

ckxllf發表於2019-12-10

  當一個物件在一條鏈上被多個攔截器攔截處理時,我們這樣的設計模式稱為責任鏈模式,它用於一個物件在多個角色中傳遞的場景。

  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();

  } 鄭州好的婦科醫院

  }

  執行結果:

  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();

  }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2667695/,如需轉載,請註明出處,否則將追究法律責任。

相關文章