ITPub博客

首页 > 应用开发 > Java > 责任链模式在SpringAOP中的使用

责任链模式在SpringAOP中的使用

原创 Java 作者:ckxllf 时间:2019-12-10 13:34:43 0 删除 编辑

  当一个对象在一条链上被多个拦截器拦截处理时,我们这样的设计模式称为责任链模式,它用于一个对象在多个角色中传递的场景。

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

  }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69945560/viewspace-2667695/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    204
  • 访问量
    262653