zoukankan      html  css  js  c++  java
  • SpringAOP源码学习总结

    1.使用@EnableAspectJAutoProxy启用aop
    在这里插入图片描述
    2.创建abstractAutoProxyCreator
       使用@EnableAspectJAutoProxy后, 可以看到会@Import(AspectJAutoProxyRegistrar.class)
    在这里插入图片描述
       AspectJAutoProxyRegistrar将名为“org.springframework.aop.config.internalAutoProxyCreator”的AnnotationAwareAspectJAutoProxyCreator 注册进容器,该proxyCreator是AbstractAdvisorAutoProxyCreator的子类。

       初始化Spring容器的时候会在refresh() -> registerBeanPostProcessors() 遍历实现ordered接口的__beanPostProcessors__的时候getBean(AspectJAwareAdvisorAutoProxyCreator). 此处成功创建了AspectJAwareAdvisorAutoProxyCreator 。该proxyCreator 是一个abstractAutoProxyCreator的子类,是beanPostProcessor的实现. 会在initializeBean的时候被触发,用于!创建代理对象。

    3.创建advistor(切面)到缓存
       在preInstantiateSingletons()阶段, 在处理internalEventListenerProcessor的时候,createBean>resolveBeforeInstantiation>applyBeanPostProcessorsBeforeInstantiation,之前创建的proxyCreator会作为其postBeanProssor,postProcessBeforeInstantiation(),获取所有@AspectJ , 并将advisors添加到缓存①。(ps:猜想此处并不一定需要该 listenerProcessor来触发。 只是第一次触发后之后直接取了缓存而已)
    注:org.springframework.context.event.internalEventListenerProcessor.internalEventListenerProcessor 实际对应EventListenerMethodProcessor.class

    4.生成代理对象
       当使用了@Aspect拦截后
    在这里插入图片描述

       context.getBean(bean)的时候会触发doCreateBean>initializeBean>applyBeanPostProcessorsBeforeInstantiation>BeanPostProcessor.postProcessBeforeInstantiation() 此处BeanPostProcessor 指的之前的proxyCreator, 会把①处获得的所有advisors通过canApply过滤, 如果剩有advisors 就说明需要使用createProxy创建代理对象,接下来就是通过jdk/cglib创建代理对象并返回。

    5.调用advice链
       当代理对象调用方法的时候, 代理对象执行intercept,通过 List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass)得到advice链,该chain 是一个倒序的chain.将chain传入new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();创建一个invocation去执行,这里是用到责任链模式,顺序如下:
    在这里插入图片描述

       1-4步的方法执行顺序:
    在这里插入图片描述

  • 相关阅读:
    文章截断显示方法
    mysql数据库基础知识和认识
    js 返回上一页和刷新以及页面跳转
    编译原理根据项目集规范族构造LR(0)分析表
    编译原理LR(0)项目集规范族的构造详解
    编译原理复习
    FIRST集合、FOLLOW集合及LL(1)文法求法
    UML时序图
    UML活动图(二)
    UML活动图(Activity Diagram)
  • 原文地址:https://www.cnblogs.com/thewindkee/p/12873163.html
Copyright © 2011-2022 走看看