spring--aop ProxyFactory图

1 篇文章 0 订阅

AOP 思维导图

enter description here

ProxyFactory UML

点击了解UML 类图

enter description here

  • ProxyConfig:代理相关的全局配置,常见的有proxyTargetClass,exposeProxy。
  • AdvisedSupport:在Spring AOP中,Advisor(切面)就是将Advice(增强)和Pointcut(切入点)连接起来的东西。此类主要支持切面相关的操作。
  • ProxyCreatorSupport:代理创建的辅助类,主要方法就是创建代理对象。
  • AopProxy通过一个Factory工厂来创建,因为不同的外部配置决定了返回的是JDK代理还是CGLIB代理。这里涉及到两种设计模式,工厂模式和策略模式。从图中可以看出,AopProxyFactory->AopProxy->Prxoy之间的结构。

Advice UML

enter description here

JdkDynamicAopProxy时序图

enter description here

责任链

这个责任链部分,时序图不知道怎么画,所以贴了代码(ps:会的教教我)。更多责任链的实现方法,责任链3中实现方法案例


//class ReflectiveMethodInvocation implements ProxyMethodInvocation

@Override
public Object proceed() throws Throwable {
	//	如果是chain中最后一个所以,调用被代理的方法;interceptorsAndDynamicMethodMatchers就是责任链中的chain
	if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
		// 其实就是反射(Mehtod.invoke)
		return invokeJoinpoint();
	}

	//获取责任链中的第currentInterceptorIndex拦截器,并把索引加1
	Object interceptorOrInterceptionAdvice =
					this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);

....此处省略非重点.....

	// 调用链中的一个拦截器
	return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}

public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice, Serializable {
	private final AfterReturningAdvice advice;
.......

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		//1. do something before if you want
		
		//2.执行下一个拦截器
		Object retVal = mi.proceed();
		
		//3. do something after if you want
		this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
		return retVal;
	}

}

参考

AOP 那点事儿(续集)

Ending

吃鸡小结:跳车会死的呀!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值