AOP整体流程
2020-11-16 23:06:44 70 举报
AOP整体流程
作者其他创作
大纲/内容
是否匹配
invocation.proceed()
获取方法上的切点表达式
否,取出interceptor
AopUtils.findAdvisorsThatCanApply
eligibleAdvisors.add(candidate)
this.advisorFactory.isAspect(beanType)
12.返回
使用切点表达式进行精筛
currentInterceptorIndex 初始值为-1interceptorsAndDynamicMethodMatchers 为interceptor的数量
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod
reset()
advisor.getAdvice()
@EnableAspectJAutoProxy
parser.parsePointcutExpression
排序
遍历完毕返回集合
introductionAwareMethodMatcher.matches
proxyFactory.getProxy
registry.registerBeanDefinition
new AspectJMethodBeforeAdvice
ReflectiveAspectJAdvisorFactory#getAdvisors
获得切点表达式
获取切点表达式
new DefaultAdvisorAdapterRegistry()
调用
new JdkDynamicAopProxy(config)
将匹配的advisor加入到集合中
instantiateAdvice
invokeAdviceMethod
JdkDynamicAopProxy#invoke
canApply
获取advisors
创建代理对象时机为初始化后
根据bean获取到匹配的advisor
创建一个最外层的MethodInvocation用于发起调用
封装成对象返回,创建对象时将会解析方法创建advice
findAdvisorsThatCanApply
获取到beanClass
new MethodBeforeAdviceInterceptor(advice)
advice本身是否就是MethodInterceptor
7. 调用通知方法
2.调用after通知
this.advisorFactory.getAdvisors(factory)
Proxy.newProxyInstance
// 获取和Aspect相关的方法进行遍历getAdvisorMethods(aspectClass)
获取到方法上的注解信息
advice.before
是,将advice强转成Interceptor放入集合
getAdvisor
new ReflectiveMethodInvocation
取出advice
判断是否匹配
发起调用
实例化出advice
createAopProxy(this)
将所有advisor中的advice取出并封装成intercept
obtainPointcutExpression
targetSource包含了实际的bean
创建代理对象
将beanFactory和beanName封装到新的Factory中
interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex)
registerBeanDefinitions
advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice
获取合适的advisor
3.afterReturning
BeanFactoryAspectJAdvisorsBuilder#buildAspectJAdvisors
initializePointcutParser
构建BeanDefinition
注册AnnotationAwareAspectJAutoProxyCreator
proxyFactory.addAdvisors(advisors);\t\tproxyFactory.setTargetSource(targetSource);
将转换的interceptor放入集合
@Import(AspectJAutoProxyRegistrar.class)
获取对应的Interceptor
缓存
创建AOP代理对象默认创建Jdk动态代理
10.返回
interceptors.add
拿出所有advisor进行遍历
interceptorList.addAll
1.调用before通知
否,continue
AspectJAwareAdvisorAutoProxyCreator#shouldSkip
否,遍历创建时注册的三个注册器
createProxy
new RootBeanDefinition(cls)
invokeJoinpoint
for (AdvisorAdapter adapter : this.adapters)
注册三个适配器
advice.afterReturning
registerAdvisorAdapter(new MethodBeforeAdviceAdapter());\t\tregisterAdvisorAdapter(new AfterReturningAdviceAdapter());\t\tregisterAdvisorAdapter(new ThrowsAdviceAdapter());
创建代理工厂
解析切面过程
shouldSkip
this.beanFactory.getType(beanName)
config.getAdvisors()
applyBeanPostProcessorsBeforeInstantiation
构建advisors
获取到所有的advisor
adapter.getInterceptor(advisor)
pc.getClassFilter().matches(targetClass)
13.返回
findEligibleAdvisors
将BeanDefinition注册到容器中
interceptors.add((MethodInterceptor) advice)
findCandidateAdvisors
buildPointcutExpression
筛选出匹配的advisor
shadowMatch.alwaysMatches()
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
advisors.add(advisor)
判断advice是哪个advice 如:(advice instanceof MethodBeforeAdvice)
adapter.supportsAdvice(advice)
ReflectionUtils.getAllDeclaredMethods
获取到所有的方法
AbstractAutoProxyCreator#postProcessBeforeInstantiation
将所有advisor中的advice取出,并转化为对于的interceptor
methodInterceptor.invoke(this)
8.返回
返回
粗筛
AbstractAutowireCapableBeanFactory#createBean
4.AfterThrowing
5.调用目标方法
获得切点解析器
将advisor中的advice转化为interceptor
sortAdvisors(eligibleAdvisors)
添加到集合中
9.调用通知方法
getPointcut
AbstractAutoProxyCreator#postProcessAfterInitialization
11.抛出异常调用
getAdvicesAndAdvisorsForBean
PointcutParser.getPointcutParserSupporting...
registry.getInterceptors(advisor)
new InstantiationModelAwarePointcutAdvisorImpl
couldMatchJoinPointsInType
将整个this传入,包含了advisors和bean
调用interceptor方法,注意这里传参
matchesMethodExecution
是否标识了@AspectJ注解
获取到advisor
6.返回结果
创建一个advisor适配器的注册器用于转化advice,创建时将默认注册三个适配器
this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1
findCandidateAdvisors()
new ProxyFactory()
先获取到所有的advisor
创建动态代理
this.aspectJAdvisorsBuilder.buildAspectJAdvisors()
obalAdvisorAdapterRegistry.getInstance()
根据注解类型创建出Adivice对象
advice instanceof MethodInterceptor
调用实例化前的处理逻辑
精筛
resolveBeforeInstantiation
mi.proceed()
0 条评论
回复 删除
下一页