Loading

03Spring AOP时序图分析

AOP:

动态代理实现AOP!增强、解耦!
JDK,跟目标类实现一个相同的接口!
Cglib直接继承目标类,重写目标方法!


Spring源码中AOP在DI之后完成!
IOC -> DI -> AOP -> MVC


AOP顶层设计:
getBean() -> ApplicationContext -> AdvisedSupport -> AopConfig -> Advice -> JdkDynamicAopProxy


目标方法与通知是一对多的关系! Map<Method, List<Advice>>
Spring源码中为
/** Cache with Method as key and advisor chain List as value */
private transient Map<MethodCacheKey, List<Object>> methodCache;

AOP主要流程:

寻找入口 -> 选择策略 -> 调用方法 -> 触发通知 

 

AOP时序图:

AOP组件:

 

 

 

1、源码中AOP入口:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean

2、调用BeanPostProcessor后置处理器实例对象初始化之后的处理方法:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization


3、org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#wrapIfNecessary


4、获取这个bean所有的advice:
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getAdvicesAndAdvisorsForBean


5、获取类中所需要的通知集合List<Advisor>:
org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#findEligibleAdvisors


6、对获取的通知Advisors进行排序设置优先级(Order):
org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#sortAdvisors


7、创建代理类:
给ProxyFactory设置Advisor数组等属性
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#createProxy


8、获取代理类:
org.springframework.aop.framework.ProxyFactory#getProxy(java.lang.ClassLoader)

 

9、创建代理类:
org.springframework.aop.framework.AopProxyFactory#createAopProxy
判断使用ObjenesisCglibAopProxy还是JdkDynamicAopProxy(都是AopProxy子类):
org.springframework.aop.framework.DefaultAopProxyFactory#createAopProxy


10、返回创建好的代理类(CglibAopProxy or JdkDynamicAopProxy):
org.springframework.aop.framework.AopProxy#getProxy(java.lang.ClassLoader)

 

10-1、org.springframework.aop.framework.JdkDynamicAopProxy#invoke
该方法中进行了:
// 获取可以应用到此方法上的Interceptor列表
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
将通知转为一个链,即org.springframework.aop.framework.AdvisedSupport配置类中的Map<MethodCacheKey, List<Object>> methodCache;
即证明Method和通知一对多!


10-2、获取所有的拦截器:
org.springframework.aop.framework.AdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice


10-3、从提供的配置实例config中获取advisor列表,遍历处理这些advisor.如果是IntroductionAdvisor,则判断此Advisor能否应用到目标类targetClass上.如果是PointcutAdvisor,则判断
此Advisor能否应用到目标方法method上.将满足条件的Advisor通过AdvisorAdaptor转化成Interceptor列表返回.
org.springframework.aop.framework.DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice
将Advisor转化成Interceptor
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
a. Interceptor是责任链的组件
b. Invocation 调用器,调用通知回调的资环,相当于责任链的上下文


11、JdkDynamicAopProxy中invocation对象代用proceed方法
org.springframework.aop.framework.ReflectiveMethodInvocation#proceed


12、去MethodInterceptor的子类
MethodBeforeAdviceInterceptor、
AfterReturningAdviceInterceptor、
AspectJAfterThrowingAdvice 去执行invoke
org.aopalliance.intercept.MethodInterceptor#invoke

 

AOP总结:
两个最核心的类
MethodInterceptor invoke()
MethodInvacation proceed()

proceed()串联起了整个Interceptor调用链!Invacation相当于责任链的上下文!

 

 

posted @ 2020-09-10 00:13  yanbubao  阅读(77)  评论(0编辑  收藏  举报