AOP
2022-03-29 23:34:09 5 举报
初试AOP,仅供参考
作者其他创作
大纲/内容
Before
AbstractAutowireCapableBeanfactoryresolveBeanInstantiation
五
wrapIfNecessary
After
AspectJExpressionPointcut
create
intercept
AspectJAfterAdvice
AOP对应beanPostProcessor
enhancer
StaticUnadvisedExposedInterceptortargetClass
方法拦截列表:ExposeInvocationIterceptor + 多个InterceptorAndDynamicMethodMatcher(MethodMatcher、MethodInterceptor)当前执行方法拦截列表位置索引:currentInterceptorIndex链式递归调用:CglibMethodInvocation -> ReflectiveMethodInvocation -> proceed() -> List<InterceptorAndDynamicMethodMatcher> list.get(++currentInterceptorIndex) -> 获取符合条件的MethodInterceptor -> invoke()
Around Before
getAdvisors
LazySingletonAspectInstanceFactoryDecorator
CglibMethodInvocationproceed()
AfterThrowingtry{ mi.proceed()} catch{ invokeAdviceMethod()}有异常时继续调用异常方法没异常向上执行
AbstractAspectAdviceinvokeAdviceMethod
AnnotationAspectJAnnotationTypeargumentNamespointcutExpression
AbstractAutoProxyCreatorpostProcessBeforeInstantiation
CglibAopProxyAdvisedSupport advisedAdvisedDispatcher(AdvisedSupport) advisedDispatcher
IOC容器
先跳过
ReflectiveAspectJAdvisorFactory
Advisor
ProxyCreatorSupportaopProxyFactory
CglibAopProxy
StaticDispatchertargetClass
@Pointcut@Around@Before@After@AfterReturning@AfterThrowing
CglibMethodInvocation
Bean Factory
interceptorList
BeanPostProcessor
ExposeInvocationInterceptor设置ReflectiveMethodInvocation到ThreadLocal
AspectJAroundAdvice
AfterReturningmi.proceed()invokeAdviceMethod()return retVal处理AfterReturning方法并返回结果
MethodBeforeAdviceInterceptor
六
AbstractAutoProxyCreatorpostProcessAfterInitialization
DefaultAdvisorChainFactorygetInterceptorsAndDynamicInterceptionAdvice
beanNames
@EnableAspectJAutoProxy
2
Advice instantiatedAdvice = AspectJAroundAdviceAspectJMethodBeforeAdviceAspectJAfterAdviceAspectJAfterReturningAdviceAspectJAfterThrowingAdvice
getAdvicesAndAdvisorsForBean
DefaultAopProxyFactorycreateAopProxy
BeanFactoryPostProcessor
BeanFactoryAspectInstanceFactory
二
5
代理类
四
MethodInterceptor
DynamicAdvisedInterceptorAdvisedSupport advised
AnnotationAwareAspectAutoProxyCreatorfindCandidateAdvisors
AfterReturning
AspectJAfterThrowingAdvice
findEligibleAdvisors
InterceptorAndDynamicMethodMatcher列表
beanPostProcessors
beanClass -> methods
ExposeInvocationInterceptor
++currentInterceptorIndexmin:= -1max:= interceptorList.size - 1
methodCache初始化后缓存起来
MethodMatcher
AspectJAnnotation
AspectAfterReturningAdvice
JdkDynamicAopProxy
拦截器列表数据来源
exist
BeanNameBeanFactory
Aftertry{ mi.proceed()} finally{ invokeAdviceMethod()}处理After方法
AfterThrowing
Aware
处理返回值
BeanInstance
BeanPostProcessor - after
Around After
AspectMetadata
聚合
DynamicAdvisedInterceptor
callback[]
AbstractAspectJAdvisorFactory
AdvisedSupportadvisorChainFactory
BeanDefinition
BeanNameBeanClassTruePointCutAjType(BeanClass)
递归返回
BeanPostProcessor - before
ReflectiveMethodInvocation代理类目标类目标对象目标方法方法参数interceptors
MethodInterceptorinvoke()
1
getProxy
beanName -> internalAutoProxyCreator
AspectJMethodBeforeAdvice
List<InterceptorAndDynamicMethodMatcher> interceptorList.get(++currentInterceptorIndex)
三
通知列表额外增加一个ExposeInvocationInterceptor.ADVISOR内部维护了一个ThreadLocal用来维护当前MethodInvocation
一
AnnotationAwareAspectJAutoProxyCreator
0
PopulateBean
目标方法
3
InstantiationModelAwarePointcutAdvisorImpl
查找携带@Aspect的bean
AopUtils.findAdvisorsThatCanApply从通知列表中获取可以处理目标类的通知
4
MethodMatchermatches()
createProxy
BeanFactoryAspectJAdvisorsBuilderbuildAspectJAdvisors
向容器中注入一个BeanDefinition当bean需要被代理时使用当前类对应的beanPostProcessor
BeanFactoryBeanClassAspectJAnnotation.pointcutExpression
6
AspectAwareAdvisorAutoProxyCreatorshouldSkip
exit
ProxyFactory
find
proceed()
InitializingBean
beanType
AfterReturningAdviceInterceptor
InitMethod
0 条评论
下一页