责任链模式在SpringAOP中怎么用

小编给大家分享一下责任链模式在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中怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网页题目:责任链模式在SpringAOP中怎么用
当前URL:http://ybzwz.com/article/piosis.html