cglib动态代理浅析

技术文档网 2021-04-25

cglib Proxy 实现原理(CglibAopProxy#getProxy)

  1. 获取该类的拦截器 Callback[] callbacks = getCallbacks(rootClass);
    1.1 切面相关的拦截器:DynamicAdvisedInterceptor
  2. 切面拦截器:DynamicAdvisedInterceptor#intercept
    2.1 interceptorsAndDynamicMethodMatchers属性初始化
    interceptorsAndDynamicMethodMatchers初始化

    2.2 interceptorsAndDynamicMethodMatchers属性初始化(判断方法是否进拦截器的关系属性)
    AspectJExpressionPointcut的调用栈
    List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
    -->DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice
       -->match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
    
    2.3 AspectJExpressionPointcut#matches(对方法进行过滤匹配)
    创建ShadowMatch
    AspectJExpressionPointcut#getShadowMatch
    -->PointcutExpressionImpl#matchesMethodExecution
       -->ShadowMatchImpl sm = this.getShadowMatch(s);
     1. FuzzyBoolean match = this.pointcut.match(forShadow);(详细分析见:https://blog.csdn.net/quyixiao/article/details/109343482)
        -->Pointcut#match
           -->ret = this.matchInternal(shadow);//KindedPointcut#matchInternal
              -->-->SignaturePattern#matches
         1.1 JoinPointSignatureIterator candidateMatches = joinPointSignature.getJoinPointSignatures(world);
           -->candidateMatches包含接口或者父类中的方法
         1.2 do..while循环匹配(如果都匹配不上返回false)
           -->SignaturePattern#matchesExactlyMethod
              --> this.declaringType.matchesStatically(aMethod.getDeclaringType().resolve(world))//WildTypePattern#matchesExactly
            (1)如果包名和类名匹配不是*,则对第一项进行匹配
            (2) 如果表达式方法参数类型只有一个参数,并且是 .. ,则直接返回 YES
    
  3. 代理拦截方法:ReflectiveMethodInvocation#proceed ``` if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
         return invokeJoinpoint();
    
    }
  4. interceptorsAndDynamicMethodMatchers(见上面分析)拦截器调完后,调用自己本身 ```

相关文章

  1. 基于-SLF4J-MDC-机制的日志链路追踪配置属性

    ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC

  2. ajax-跨域访问

    ajax 跨域访问 &lt;!DOCTYPE html&gt; &lt;html xmlns:th="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt;

  3. 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache

    spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port

  4. Java动态代理

    Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public

  5. Java读取classpath中的文件

    public void init() { try { //URL url = Thread.currentThread().getContextClassLo

随机推荐

  1. 基于-SLF4J-MDC-机制的日志链路追踪配置属性

    ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC

  2. ajax-跨域访问

    ajax 跨域访问 &lt;!DOCTYPE html&gt; &lt;html xmlns:th="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt;

  3. 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache

    spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port

  4. Java动态代理

    Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public

  5. Java读取classpath中的文件

    public void init() { try { //URL url = Thread.currentThread().getContextClassLo