Spring AOP时序图
2020-03-13 18:50:05 0 举报
Spring AOP时序图,结合XML总结AOP实现流程,包括Advisor,pointcut,Advice初始化的时机以及proxy生成和调用流程
作者其他创作
大纲/内容
proxyFactory.addAdvisors(advisors);
method.invoke()
AbstractXmlApplicationContext
getInstance
AbstractAutoProxyCreator。postProcessBeforeInstantiation()主要是往advisedBeans里面添加advisor的beanName注意shouldSkip() -> AbstractAdvisorAutoProxyCreator.findCandidateAdvisors提前初始化Advisor的bean(populateBean属性pointcut,pointcut是prototype的)而Advice的bean是在warpIfNecessary() -> findCandidateAdvisors() -> advisor.getAdvice()的时候初始化的
AbstractAutowireCapableBeanFactory
findAdvisorBeans()
doCreateBean()
BeanFactoryAdvisorRetrievalHelper
postProcessAfterInitialization()
在获得inteceptor的list之后如果list是空的,则直接通过method.invoke()调用代理对象的方法。如果list不是空的,则通过ReflectiveMethodInvocation递归调用inteceptor和 以及最后调用method.invoke()(注意AfterReturning和AfterThrowingAdvice)
getAdvicesAndAdvisorsForBean()
createAopProxy(this)
applyBeanPostProcessorsAfterInitialization()
ReflectiveMethodInvocation
getInterceptors()
initializeBean()
applyBeanPostProcessorsBeforeInitialization()
ProxyCreatorSupport
createProxy()
getProxy(classLoader)
if advisor is PointcutAdvisor
createAopProxy()
new proxyFactory.getProxy(getProxyClassLoader())
根据Advisor的beanName,通过beanFactory.getBean()来获得对应Advisor的Bean
如果用了aop:advisor或者aop:advice,则通过ConfigBeanDefinitionParser来parsePointcut,parseAdvisor,parseAspect,然后registerBeanDefinition..(之前一步会configureAutoProxyCreator,即注册internalAutoProxyCreator到beanDefinition,internalAutoProxyCreator是AspectJAwareAdvisorAutoProxyCreator,是一个BeanPostProcessor)。
AbstractAdvisorAutoProxyCreator
new JdkDynamicAopProxy(config)
populateBean()
getInterceptorsAndDynamicInterceptionAdvice()
invocation.proceed()
invoke()
JdkDynamicAopProxy
invokeInitMethods()
font color=\"#ff3333\
wrapIfNecessary()
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { return invokeJoinpoint(); } Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);如果有一个MethodBeforeAdvice和一个MethodAfterAdvice先调用MethodBeforeAdvice.before(),然后调用MethodAfterAdvice之前先进行proceed,这时候this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1),调用invokeJoinpoint(),即method.invoke()调用真正方法,返回之后调用MethodAfterAdvice.afterReturning()方法,完成切入
AbstractAutoProxyCreator
AdvisedSupport
beanNamesForTypeIncludingAncestors()找到Advisor的beanName
postProcessBeforeInstantiation()
GlobalAdvisorAdapterRegistry.getInstance()单例模式,生成DefaultAdvisorAdapterRegistry该类构造函数:registerAdvisorAdapter(new MethodBeforeAdviceAdapter());registerAdvisorAdapter(new AfterReturningAdviceAdapter());registerAdvisorAdapter(new ThrowsAdviceAdapter());将这三个AdvisorAdapter添加到ArrayList类型的adapters里面这些adapter适配器的实现为AOP的Advice提供了编织能力
loadBeanDefinitions()
postProcessBeforeInitialization()
DefaultAdvisorChainFactory
findCandidateAdvisors()
findEligibleAdvisors()
extendAdvisors()
SpringAOP:ProxyConfig生成代理类的父类。下面实现类有AdvisedSupport,AbstractAutoProxyCreatorAdvisedSupport同时实现了Advised,来对Advisor进行add或者remove-- ProxyFactory是主要是实现类-- AspectJProxyFactory主要提供了PointCut切入点,ProxyFactory则是对所有方法生效。AbstractAutoProxyCreator则是最主要创建proxy的类Advised - 包含Advisor和Advice是AdvisedSupport,ProxyFactory,AspectJProxyFactory的父类Advice - 通知,有前置通知,后置通知等等。(Inteceptor的实现类MethodInteceptor也是Advice)Pointcut - 切入点Advisor - 通知+切入点的适配器,可以定位到PointCut所指向的方法(AspectJExpressionPointcutAdvisor)Advice也能包装成Advisor
createBean()
ProxyFactory
getInterceptors()如果advice是MethodInteceptor,则直接加入list如果不是则通过上面new的三个AdvisorAdapter,对advice进行适配,比如MethodBeforeAdvice会被适配成MethodBeforeAdviceInterceptor,然后加入list
BeanFactoryUtils.beanNamesForTypeIncludingAncestors()
AbstractAutoProxyCreator。postProcessBeforeInitialization()没做任何处理
getAopProxyFactory()
resolveBeforeInstantiation()
Pointcut切点进行matches的匹配过程,如果match的,则需要增强
DefaultAopProxyFactory
applyBeanPostProcessorsBeforeInstantiation()
createBeanInstance()
DefaultAdvisorAdapterRegistry
0 条评论
回复 删除
下一页