小编给大家分享一下责任链模式在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中怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: 责任链模式在SpringAOP中怎么用
本文链接: https://lsjlt.com/news/230216.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0