动态代理--基础--03--cglib原理细节

动态代理–基础–03–cglib原理细节


代码位置

https://gitee.com/DanShenGuiZu/learnDemo/tree/master/动态代理/demo1

1、代码实现


package feizhou.demo1.business.demo.cglib;

import feizhou.demo1.business.demo.jdk.UserDao;
import feizhou.demo1.business.demo.jdk.UserImpl;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Cglib动态代理,实现MethodInterceptor接口
 */
public class CglibProxy implements MethodInterceptor {
    private Object target;//需要代理的目标对象


    //重写拦截方法
    @Override
    public Object intercept(Object obj, Method method, Object[] arr, MethodProxy proxy) throws Throwable {
        System.out.println("Cglib动态代理,监听开始!");
//        Object invoke = method.invoke(target, arr);//方法执行,参数:target 目标对象 arr参数数组
        Object invoke = proxy.invokeSuper(obj, arr);
        System.out.println("Cglib动态代理,监听结束!");
        return invoke;
    }

    public CglibProxy(Object target) {
        this.target = target;
    }


    public static void main(String[] args) {

        UserImpl userImpl = new UserImpl();

        //该设置用于输出cglib动态代理产生的类
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "H:\\test");
        //实例化CglibProxy对象
        CglibProxy cglib = new CglibProxy(userImpl);
        //定义获取代理对象方法
        Enhancer enhancer = new Enhancer();
        //设置父类,因为Cglib是针对指定的类生成一个子类,所以需要指定父类
        enhancer.setSuperclass(userImpl.getClass());
        // 设置回调
        enhancer.setCallback(cglib);
        //创建并返回代理对象
        UserDao userDao = (UserDao) enhancer.create();
        //执行add方法
        userDao.addUser();
    }

}

1.1、Enhancer

  • 用于创建代理类对象
  • 是CGLIB的字节码增强器,可以很方便的对类进行拓展

1.2、创建代理对象的几个步骤

  • 生成代理类的二进制字节码文件
  • 加载二进制字节码,生成Class对象(例如使用Class.forName()方法)
  • 通过反射机制获得实例构造,并创建代理类对象

2、cglib生成的class文件

package cn.feizhou.Test.jdk;

import java.lang.reflect.Method;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class UserImpl$$EnhancerByCGLIB$$70e327a6 extends UserImpl
  implements Factory
{
  private boolean CGLIB$BOUND;
  public static Object CGLIB$FACTORY_DATA;
  private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
  private static final Callback[] CGLIB$STATIC_CALLBACKS;
  private MethodInterceptor CGLIB$CALLBACK_0;
  private static Object CGLIB$CALLBACK_FILTER;
  private static final Method CGLIB$addUser$0$Method;
  private static final MethodProxy CGLIB$addUser$0$Proxy;
  private static final Object[] CGLIB$emptyArgs;
  private static final Method CGLIB$equals$1$Method;
  private static final MethodProxy CGLIB$equals$1$Proxy;
  private static final Method CGLIB$toString$2$Method;
  private static final MethodProxy CGLIB$toString$2$Proxy;
  private static final Method CGLIB$hashCode$3$Method;
  private static final MethodProxy CGLIB$hashCode$3$Proxy;
  private static final Method CGLIB$clone$4$Method;
  private static final MethodProxy CGLIB$clone$4$Proxy;

  static void CGLIB$STATICHOOK1()
  {
    CGLIB$THREAD_CALLBACKS = new ThreadLocal();
    CGLIB$emptyArgs = new Object[0];
    Class localClass1 = Class.forName("cn.feizhou.Test.jdk.UserImpl$$EnhancerByCGLIB$$70e327a6");
    Class localClass2;
    Method[] tmp50_47 = ReflectUtils.findMethods(new String[] { "addUser", "()V" }, (localClass2 = Class.forName("cn.feizhou.Test.jdk.UserImpl")).getDeclaredMethods());
    CGLIB$addUser$0$Method = tmp50_47[0];
    CGLIB$addUser$0$Proxy = MethodProxy.create(localClass2, localClass1, "()V", "addUser", "CGLIB$addUser$0");
    tmp50_47;
    Method[] tmp131_128 = ReflectUtils.findMethods(new String[] { "equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;" }, (localClass2 = Class.forName("java.lang.Object")).getDeclaredMethods());
    CGLIB$equals$1$Method = tmp131_128[0];
    CGLIB$equals$1$Proxy = MethodProxy.create(localClass2, localClass1, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
    Method[] tmp151_131 = tmp131_128;
    CGLIB$toString$2$Method = tmp151_131[1];
    CGLIB$toString$2$Proxy = MethodProxy.create(localClass2, localClass1, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
    Method[] tmp171_151 = tmp151_131;
    CGLIB$hashCode$3$Method = tmp171_151[2];
    CGLIB$hashCode$3$Proxy = MethodProxy.create(localClass2, localClass1, "()I", "hashCode", "CGLIB$hashCode$3");
    Method[] tmp191_171 = tmp171_151;
    CGLIB$clone$4$Method = tmp191_171[3];
    CGLIB$clone$4$Proxy = MethodProxy.create(localClass2, localClass1, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
    tmp191_171;
  }

  final void CGLIB$addUser$0()
  {
    super.addUser();
  }

  public final void addUser()
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    if (this.CGLIB$CALLBACK_0 != null)
      return;
    super.addUser();
  }

  final boolean CGLIB$equals$1(Object paramObject)
  {
    return super.equals(paramObject);
  }

  public final boolean equals(Object paramObject)
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
    {
      Object tmp41_36 = tmp17_14.intercept(this, CGLIB$equals$1$Method, new Object[] { paramObject }, CGLIB$equals$1$Proxy);
      tmp41_36;
      return tmp41_36 == null ? false : ((Boolean)tmp41_36).booleanValue();
    }
    return super.equals(paramObject);
  }

  final String CGLIB$toString$2()
  {
    return super.toString();
  }

  public final String toString()
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
      return (String)tmp17_14.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy);
    return super.toString();
  }

  final int CGLIB$hashCode$3()
  {
    return super.hashCode();
  }

  public final int hashCode()
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
    {
      Object tmp36_31 = tmp17_14.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
      tmp36_31;
      return tmp36_31 == null ? 0 : ((Number)tmp36_31).intValue();
    }
    return super.hashCode();
  }

  final Object CGLIB$clone$4()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  protected final Object clone()
    throws CloneNotSupportedException
  {
    MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
    if (tmp4_1 == null)
    {
      tmp4_1;
      CGLIB$BIND_CALLBACKS(this);
    }
    MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
    if (tmp17_14 != null)
      return tmp17_14.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy);
    return super.clone();
  }

  public static MethodProxy CGLIB$findMethodProxy(Signature paramSignature)
  {
    String tmp4_1 = paramSignature.toString();
    switch (tmp4_1.hashCode())
    {
    case -508378822:
      if (tmp4_1.equals("clone()Ljava/lang/Object;"))
        return CGLIB$clone$4$Proxy;
      break;
    case 1761046505:
    case 1826985398:
    case 1913648695:
    case 1984935277:
    }
  }

  public UserImpl$$EnhancerByCGLIB$$70e327a6()
  {
    CGLIB$BIND_CALLBACKS(this);
  }

  public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] paramArrayOfCallback)
  {
    CGLIB$THREAD_CALLBACKS.set(paramArrayOfCallback);
  }

  public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] paramArrayOfCallback)
  {
    CGLIB$STATIC_CALLBACKS = paramArrayOfCallback;
  }

  private static final void CGLIB$BIND_CALLBACKS(Object paramObject)
  {
    // Byte code:
    //   0: aload_0
    //   1: checkcast 2	cn/feizhou/Test/jdk/UserImpl$$EnhancerByCGLIB$$70e327a6
    //   4: astore_1
    //   5: aload_1
    //   6: getfield 187	cn/feizhou/Test/jdk/UserImpl$$EnhancerByCGLIB$$70e327a6:CGLIB$BOUND	Z
    //   9: ifne +43 -> 52
    //   12: aload_1
    //   13: iconst_1
    //   14: putfield 187	cn/feizhou/Test/jdk/UserImpl$$EnhancerByCGLIB$$70e327a6:CGLIB$BOUND	Z
    //   17: getstatic 27	cn/feizhou/Test/jdk/UserImpl$$EnhancerByCGLIB$$70e327a6:CGLIB$THREAD_CALLBACKS	Ljava/lang/ThreadLocal;
    //   20: invokevirtual 190	java/lang/ThreadLocal:get	()Ljava/lang/Object;
    //   23: dup
    //   24: ifnonnull +15 -> 39
    //   27: pop
    //   28: getstatic 185	cn/feizhou/Test/jdk/UserImpl$$EnhancerByCGLIB$$70e327a6:CGLIB$STATIC_CALLBACKS	[Lorg/springframework/cglib/proxy/Callback;
    //   31: dup
    //   32: ifnonnull +7 -> 39
    //   35: pop
    //   36: goto +16 -> 52
    //   39: checkcast 191	[Lorg/springframework/cglib/proxy/Callback;
    //   42: aload_1
    //   43: swap
    //   44: iconst_0
    //   45: aaload
    //   46: checkcast 51	org/springframework/cglib/proxy/MethodInterceptor
    //   49: putfield 39	cn/feizhou/Test/jdk/UserImpl$$EnhancerByCGLIB$$70e327a6:CGLIB$CALLBACK_0	Lorg/springframework/cglib/proxy/MethodInterceptor;
    //   52: return
  }

  public Object newInstance(Callback[] paramArrayOfCallback)
  {
    CGLIB$SET_THREAD_CALLBACKS(paramArrayOfCallback);
    CGLIB$SET_THREAD_CALLBACKS(null);
    return new 70e327a6();
  }

  public Object newInstance(Callback paramCallback)
  {
    CGLIB$SET_THREAD_CALLBACKS(new Callback[] { paramCallback });
    CGLIB$SET_THREAD_CALLBACKS(null);
    return new 70e327a6();
  }

  public Object newInstance(Class[] paramArrayOfClass, Object[] paramArrayOfObject, Callback[] paramArrayOfCallback)
  {
    CGLIB$SET_THREAD_CALLBACKS(paramArrayOfCallback);
    Class[] tmp9_8 = paramArrayOfClass;
    switch (tmp9_8.length)
    {
    case 0:
      tmp9_8;
      break;
    default:
      new 70e327a6();
      throw new IllegalArgumentException("Constructor not found");
    }
    CGLIB$SET_THREAD_CALLBACKS(null);
  }

  public Callback getCallback(int paramInt)
  {
    CGLIB$BIND_CALLBACKS(this);
    switch (paramInt)
    {
    case 0:
      break;
    }
    return null;
  }

  public void setCallback(int paramInt, Callback paramCallback)
  {
    switch (paramInt)
    {
    case 0:
      this.CGLIB$CALLBACK_0 = ((MethodInterceptor)paramCallback);
      break;
    }
  }

  public Callback[] getCallbacks()
  {
    CGLIB$BIND_CALLBACKS(this);
    return new Callback[] { this.CGLIB$CALLBACK_0 };
  }

  public void setCallbacks(Callback[] paramArrayOfCallback)
  {
    this.CGLIB$CALLBACK_0 = ((MethodInterceptor)paramArrayOfCallback[0]);
  }

  static
  {
    CGLIB$STATICHOOK1();
  }
}

2.1、根据我们业务类的每一个方法都生成了2个代理方法

  • 第1个代理方法直接调用父类的方法,也就是我们业务类的方法
  • 第2个方法也就是代理类真正调用的方法是经过封装的,他会去判断是否实现了MethodInterceptor 的intercept方法,如果实现了则会调用intercept方法。这个MethodInterceptor 就是我们在enhancer.setCallback(cglib);这边设置进去的。

从我们定义的拦截器的intercept方法中可以看到Object invoke = proxy.invokeSuper(obj, arr);

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值