JDK動態代理和 CGLIB 代理

xjzcz發表於2020-07-27

JDK動態代理和 CGLIB 代理

JDK動態代理:其代理物件必須是某個介面的實現,它是通過在執行期期間建立一個介面的實現類來完成對目標物件的代理。

程式碼示例

介面

public interface IUserDao {
    void save();
}

實現類

public class UserDao implements IUserDao {

    @Override
    public void save() {
        System.out.println("=====已經儲存資料=======");
    }
}

代理類

public class DynamicJdkProxy {
    /**
     * 維護一個目標物件
     */
    private Object target;

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

    /**
     * 給目標物件生成代理物件
     *
     * @return
     */
    public Object getProxyInstance() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("開始事務2");
                        //執行目標物件方法
                        Object returnValue = method.invoke(target, args);
                        System.out.println("提交事務2");
                        return returnValue;
                    }
                }
        );
    }
}

執行

        //生成相應的class檔案
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        // 目標物件
        IUserDao target = new UserDao();
        // 【原始的型別 class com.example.learning.proxy.UserDao】
        System.out.println(target.getClass());

        // 給目標物件,建立代理物件
        IUserDao proxy = (IUserDao) new DynamicJdkProxy(target).getProxyInstance();
        // class $Proxy0   記憶體中動態生成的代理物件
        System.out.println(proxy.getClass());

        // 執行方法   【代理物件】
        proxy.save();

執行結果

class com.example.learning.proxy.UserDao
class com.sun.proxy.$Proxy0
開始事務2
=====已經儲存資料=======
提交事務2

Process finished with exit code 0

原始碼分析:

 public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {

        //克隆介面
        final Class<?>[] intfs = interfaces.clone();
        //重點:生成 $Proxy0.class 檔案並通過 ClassLoader 載入進來
        Class<?> cl = getProxyClass0(loader, intfs);
        //建立代理類的構造器
        final Constructor<?> cons = cl.getConstructor(constructorParams);
        //生成代理類的例項
        return cons.newInstance(new Object[]{h});

    }

再來看 getProxyClass0 的具體實現:ProxyClassFactory工廠類:

    Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
            for (Class<?> intf : interfaces) {
                    interfaceClass = Class.forName(intf.getName(), false, loader);
            }

            String proxyPkg = null;     // package to define proxy class in

            //如果需要代理的類中有非public函式,直接取代理類的路徑
            for (Class<?> intf : interfaces) {
                int flags = intf.getModifiers();
                if (!Modifier.isPublic(flags)) {
                    accessFlags = Modifier.FINAL;
                    String name = intf.getName();
                    int n = name.lastIndexOf('.');
                    String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
                    if (proxyPkg == null) {
                        proxyPkg = pkg;
                    } else if (!pkg.equals(proxyPkg)) {
                        throw new IllegalArgumentException(
                            "non-public interfaces from different packages");
                    }
                }
            }
            
            //如果全是 Public 函式,取com.sun.proxy 作為包名
            if (proxyPkg == null) {
                // if no non-public proxy interfaces, use com.sun.proxy package
                proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
            }

            /*
             *  生成代理類名稱 com.sun.proxy.$ProxyXXX
             */
            long num = nextUniqueNumber.getAndIncrement();
            String proxyName = proxyPkg + proxyClassNamePrefix + num;

            /*
             * 產生代理類class
             */
            byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
                proxyName, interfaces, accessFlags);
            //載入class
           return defineClass0(loader, proxyName,
                                    proxyClassFile, 0, proxyClassFile.length);
        }

生成class

    public static byte[] generateProxyClass(final String var0, Class<?>[] var1, int var2) {
        ProxyGenerator var3 = new ProxyGenerator(var0, var1, var2);
        final byte[] var4 = var3.generateClassFile();
        if (saveGeneratedFiles) {
        ...
        }

是否生成class標識

private static final boolean saveGeneratedFiles = 
(Boolean)AccessController.doPrivileged(new GetBooleanAction("sun.misc.ProxyGenerator.saveGeneratedFiles"));

生成後的代理類:

public final class $Proxy0 extends Proxy implements IUserDao {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    private static Method m0;

    public $Proxy0(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        //省略
    }

    public final void save() throws  {
        try {
            super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String toString() throws  {
        //省略
    }

    public final int hashCode() throws  {
        //省略
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m3 = Class.forName("com.example.learning.proxy.IUserDao").getMethod("save");
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

核心程式碼

呼叫的就是InvocationHandler#invoke方法
 super.h.invoke(this, m3, (Object[])null);
 
 h為Proxy類的
   /**
     * the invocation handler for this proxy instance.
     * @serial
     */
    protected InvocationHandler h;

JDK 代理通過在執行期期間建立一個介面的實現類來完成對目標物件的代理.

CGLIB 代理

CGLIB代理:實現原理類似於JDK動態代理,只是它在執行期間生成的代理物件是針對目標類擴充套件的子類。CGLIB是高效的程式碼生成包,底層依靠ASM(開源的Java位元組碼編輯類庫)操作位元組碼實現。

代理類

public class UserDaoCG {
    public void save() {
        System.out.println("=====已經儲存資料=======");
    }
}

CG 代理

public class CgLibProxy implements MethodInterceptor {
    /**
     * 維護目標物件
     */
    private Object target;

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

    /**
     * 給目標物件建立一個代理物件
     *
     * @return
     */
    public Object getProxyInstance() {
        //1.工具類
        Enhancer en = new Enhancer();
        //2.設定父類
        en.setSuperclass(target.getClass());
        //3.設定回撥函式
        en.setCallback(this);
        //4.建立子類(代理物件)
        return en.create();

    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("開始CGLib事務...");

        //執行目標物件的方法
        Object returnValue = method.invoke(target, args);

        System.out.println("提交CGLib事務...");

        return returnValue;
    }
}

執行代理

        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/xxx/IdeaProjects/learning/cg");
        //目標物件
        UserDao target = new UserDao();

        //代理物件
        UserDao proxy = (UserDao) new CgLibProxy(target).getProxyInstance();

        //執行代理物件的方法
        proxy.save();

執行結果

CGLIB debugging enabled, writing to '/Users/xxx/IdeaProjects/learning/cg'
開始CGLib事務...
=====已經儲存資料=======
提交CGLib事務...

生成的代理類

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.learning.proxy;

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 UserDao$$EnhancerByCGLIB$$6efc5e8b extends UserDao 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$save$0$Method;
    private static final MethodProxy CGLIB$save$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 var0 = Class.forName("com.example.learning.proxy.UserDao$$EnhancerByCGLIB$$6efc5e8b");
        Class var1;
        Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
        CGLIB$equals$1$Method = var10000[0];
        CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
        CGLIB$toString$2$Method = var10000[1];
        CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
        CGLIB$hashCode$3$Method = var10000[2];
        CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
        CGLIB$clone$4$Method = var10000[3];
        CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
        CGLIB$save$0$Method = ReflectUtils.findMethods(new String[]{"save", "()V"}, (var1 = Class.forName("com.example.learning.proxy.UserDao")).getDeclaredMethods())[0];
        CGLIB$save$0$Proxy = MethodProxy.create(var1, var0, "()V", "save", "CGLIB$save$0");
    }

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

    public final void save() {
        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
        if (var10000 == null) {
            CGLIB$BIND_CALLBACKS(this);
            var10000 = this.CGLIB$CALLBACK_0;
        }

        if (var10000 != null) {
            var10000.intercept(this, CGLIB$save$0$Method, CGLIB$emptyArgs, CGLIB$save$0$Proxy);
        } else {
            super.save();
        }
    }

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

    public final boolean equals(Object var1) {
        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
        if (var10000 == null) {
            CGLIB$BIND_CALLBACKS(this);
            var10000 = this.CGLIB$CALLBACK_0;
        }

        if (var10000 != null) {
            Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);
            return var2 == null ? false : (Boolean)var2;
        } else {
            return super.equals(var1);
        }
    }

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

    public final String toString() {
        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
        if (var10000 == null) {
            CGLIB$BIND_CALLBACKS(this);
            var10000 = this.CGLIB$CALLBACK_0;
        }

        return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();
    }

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

    public final int hashCode() {
        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
        if (var10000 == null) {
            CGLIB$BIND_CALLBACKS(this);
            var10000 = this.CGLIB$CALLBACK_0;
        }

        if (var10000 != null) {
            Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
            return var1 == null ? 0 : ((Number)var1).intValue();
        } else {
            return super.hashCode();
        }
    }

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

    protected final Object clone() throws CloneNotSupportedException {
        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
        if (var10000 == null) {
            CGLIB$BIND_CALLBACKS(this);
            var10000 = this.CGLIB$CALLBACK_0;
        }

        return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();
    }

    public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
        String var10000 = var0.toString();
        switch(var10000.hashCode()) {
        case -508378822:
            if (var10000.equals("clone()Ljava/lang/Object;")) {
                return CGLIB$clone$4$Proxy;
            }
            break;
        case 1826985398:
            if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
                return CGLIB$equals$1$Proxy;
            }
            break;
        case 1872760024:
            if (var10000.equals("save()V")) {
                return CGLIB$save$0$Proxy;
            }
            break;
        case 1913648695:
            if (var10000.equals("toString()Ljava/lang/String;")) {
                return CGLIB$toString$2$Proxy;
            }
            break;
        case 1984935277:
            if (var10000.equals("hashCode()I")) {
                return CGLIB$hashCode$3$Proxy;
            }
        }

        return null;
    }

    public UserDao$$EnhancerByCGLIB$$6efc5e8b() {
        CGLIB$BIND_CALLBACKS(this);
    }

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

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

    private static final void CGLIB$BIND_CALLBACKS(Object var0) {
        UserDao$$EnhancerByCGLIB$$6efc5e8b var1 = (UserDao$$EnhancerByCGLIB$$6efc5e8b)var0;
        if (!var1.CGLIB$BOUND) {
            var1.CGLIB$BOUND = true;
            Object var10000 = CGLIB$THREAD_CALLBACKS.get();
            if (var10000 == null) {
                var10000 = CGLIB$STATIC_CALLBACKS;
                if (var10000 == null) {
                    return;
                }
            }

            var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
        }

    }

    public Object newInstance(Callback[] var1) {
        CGLIB$SET_THREAD_CALLBACKS(var1);
        UserDao$$EnhancerByCGLIB$$6efc5e8b var10000 = new UserDao$$EnhancerByCGLIB$$6efc5e8b();
        CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
        return var10000;
    }

    public Object newInstance(Callback var1) {
        CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
        UserDao$$EnhancerByCGLIB$$6efc5e8b var10000 = new UserDao$$EnhancerByCGLIB$$6efc5e8b();
        CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
        return var10000;
    }

    public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
        CGLIB$SET_THREAD_CALLBACKS(var3);
        UserDao$$EnhancerByCGLIB$$6efc5e8b var10000 = new UserDao$$EnhancerByCGLIB$$6efc5e8b;
        switch(var1.length) {
        case 0:
            var10000.<init>();
            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
            return var10000;
        default:
            throw new IllegalArgumentException("Constructor not found");
        }
    }

    public Callback getCallback(int var1) {
        CGLIB$BIND_CALLBACKS(this);
        MethodInterceptor var10000;
        switch(var1) {
        case 0:
            var10000 = this.CGLIB$CALLBACK_0;
            break;
        default:
            var10000 = null;
        }

        return var10000;
    }

    public void setCallback(int var1, Callback var2) {
        switch(var1) {
        case 0:
            this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
        default:
        }
    }

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

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

    static {
        CGLIB$STATICHOOK1();
    }
}

關鍵程式碼

 public final void save() {
        //代理類
        MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
        if (var10000 == null) {
            CGLIB$BIND_CALLBACKS(this);
            var10000 = this.CGLIB$CALLBACK_0;
        }
        //如果代理類不為空,直接呼叫代理方法
        if (var10000 != null) {
            var10000.intercept(this, CGLIB$save$0$Method, CGLIB$emptyArgs, CGLIB$save$0$Proxy);
        } else {
            super.save();
        }
    }

由此可見:
執行期間生成的代理物件是針對目標類擴充套件的子類

在平時應用中,JDK 的動態代理要求代理的類必須有介面,這個限制了他的部分應用, 而 CGLIB
用繼承的方式代理,擺脫了這個限制.

Spring 預設的代理為 CGLIB

相關文章