1. 程式人生 > >Java 靜態代理和動態代理的使用及原理解析

Java 靜態代理和動態代理的使用及原理解析

代理模式是軟體開發中常見的設計模式,它的目的是讓呼叫者不用持有具體操作者的引用,而是通過代理者去對具體操作者執行具體的操作。

靜態代理的實現

操作介面:

public interface Operate {
    void doSomething();
}
複製程式碼

操作者:

public class Operator implements Operate {
    @Override
    public void doSomething() {
        System.out.println("I'm doing something");
    }
}
複製程式碼

代理者:

public class OperationProxy implements Operate {
    private Operator operator = null;

    @Override
    public void doSomething() {
        beforeDoSomething();
        if(operator == null){
            operator =  new Operator();
        }
        operator.doSomething();
        afterDoSomething();
    }

    private void beforeDoSomething
() { System.out.println("before doing something"); } private void afterDoSomething() { System.out.println("after doing something"); } } 複製程式碼

呼叫者:

public class StaticProxyTest {
    public static void main(String[] args) {
        Operate operate = new OperationProxy();//使用OperationProxy代替Operator
        operate.doSomething();  //代理者代替真實者做事情
    }
}
複製程式碼

靜態代理的侷限性

可以看到,靜態代理讓呼叫者不用再直接持有操作者的引用,而是將一切操作交由代理者去完成。但是靜態代理也有它的侷限性:

  1. 如果需要增加一個需要代理的方法,代理者的程式碼也必須改動進而適配新的操作;
  2. 如果需要代理者代理另外一個操作者,同樣需要對代理者進行擴充套件並且更加麻煩。

可能有人想到可以用策略模式和工廠模式分別解決上面兩個問題,但是,有沒有更加巧妙的方法呢?首先,我們瞭解一下 Java 程式碼的執行過程。

理解 Java 程式碼執行流程

要從根本上理解動態代理的實現原理,得先從 Java 程式碼的執行流程說起:

java 程式碼執行流程.jpg

JVM 在執行 .class 檔案之前,首先通過 ClassLoader 將 .class 檔案以二進位制的形式解析並生成例項以供呼叫,我們的程式碼執行邏輯是在 JVM 的執行期系統中進行工作的,那麼,我們可不可以在自己的程式碼裡面按照 .class 的格式生成自己的 .class 檔案,進而呼叫自定義的 ClassLoader 將其加載出來呢?答案是肯定的,這樣我們就可以動態地建立一個類了。

動態代理的思路.jpg

生成自己的 .class 檔案

當然我們不用手動去一點一點拼裝 .class 檔案,目前比較常用的位元組碼生成工具有 ASMJavassist,根據這個思路,生成 .class 檔案的過程如下:

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
 
public class Test {
    public static void main(String[] args) throws Exception {
        ClassPool pool = ClassPool.getDefault();
        //建立 AutoGenerateClass 類
        CtClass cc= pool.makeClass("com.guanpj.AutoGenerateClass");
        //定義 show 方法
        CtMethod method = CtNewMethod.make("public void show(){}", cc);
        //插入方法程式碼
        method.insertBefore("System.out.println(\"I'm just test generate .class file by javassit.....\");");
        cc.addMethod(method);
        //儲存生成的位元組碼
        cc.writeFile("D://temp");
    }
}
複製程式碼

生成的 .class 檔案如下:

AutoGenerate.png

反編譯後檢視內容:

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

package com.guanpj;

public class AutoGenerateClass {
    public void show() {
        System.out.println("I'm just test generate .class file by javassit.....");
    }

    public AutoGenerateClass() {
    }
}
複製程式碼

可以看到,javassit 生成的類中,除了 show() 方法之外還預設生成了一個無參的構造方法。

自定義類載入器載入

為了能夠讓自定的類被加載出來,我們自定義了一個類載入器來載入指定的 .class 檔案:

public class CustomClassLoader extends ClassLoader {

    public CustomClassLoader() {
    }

    protected Class<?> findClass(String className) {
        String path = "D://temp//" + className.replace(".","//") + ".class";
        byte[] classData = getClassData(path);
        return defineClass(className, classData, 0, classData.length);
    }

    private byte[] getClassData(String path) {
        try {
            InputStream ins = new FileInputStream(path);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int bufferSize = 4096;
            byte[] buffer = new byte[bufferSize];
            int bytesNumRead = 0;
            while ((bytesNumRead = ins.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesNumRead);
            }
            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
複製程式碼

接著,用 ClassLoader 載入剛才生成的 .class 檔案:

public class TestLoadClass {
    public static void main(String[] args) throws Exception {
        CustomClassLoader classLoader = new CustomClassLoader();
        Class clazz = classLoader.findClass("com.guanpj.AutoGenerateClass");

        Object object = clazz.newInstance();
        Method showMethod = clazz.getMethod("show", null);
        showMethod.invoke(object, null);
    }
}
複製程式碼

後臺輸出如下:

output.png

成功執行了 show 方法!

利用 JDK 中的 Proxy 類進行動態代理

使用動態代理的初衷是簡化程式碼,不管是 ASM 還是 Javassist,在進行動態代理的時候操作還是不夠簡便,這也違背了我們的初衷。我們來看一下怎麼 InvocationHandler 怎麼做:

InvocationHandler:

public class InvocationHandlerImpl implements InvocationHandler {
    Operate operate;

    //注入操作者物件
    public InvocationHandlerImpl(Operate operate) {
        this.operate = operate;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before calling method: " + method.getName());
        //呼叫操縱者的具體操作方法
        method.invoke(operate, args);
        System.out.println("after calling method: " + method.getName());
        return null;
    }
}
複製程式碼

呼叫者:

public class DynamicProxyTest {
    public static void main(String[] args) {
        //例項化操作者
        Operate operate = new Operator();
        //將操作者物件進行注入
        InvocationHandlerImpl handler = new InvocationHandlerImpl(operate);
        //生成代理物件
        Operate operationProxy = (Operate) Proxy.newProxyInstance(operate.getClass().getClassLoader(),
                operate.getClass().getInterfaces(), handler);
        //呼叫操作方法
        operationProxy.doSomething();
    }
}
複製程式碼

跟靜態代理不同的是,動態代理的過程主要分為三個步驟

  • 將操作者物件注入 InvocationHandlerImpl 類中。
  • 將 InvocationHandlerImpl 物件注入 Proxy 類中並返回代理者物件,並在 invoke 方法中進行額外的操作
  • 呼叫代理物件的操作方法

利用 CGLIB 進行動態代理

用 Proxy 類生成代理類的方法為 newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) ,第二個引數是操作者的介面陣列,意味著只能代理它實現的接口裡的方法,對於本來在操作者類中定義的方法表示無能為力,CGLIB(Code Generation Library) 解決了這個問題。

MethodInterceptorImpl:

public class MethodInterceptorImpl implements MethodInterceptor {
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("before calling method:" + method.getName());
        proxy.invokeSuper(obj, args);
        System.out.println("after calling method:" + method.getName());
        return null;
    }
}
複製程式碼

呼叫者:

public class ProxyTest {
    public static void main(String[] args) {
        Operator operator = new Operator();
        MethodInterceptorImpl methodInterceptorImpl = new MethodInterceptorImpl();

        //初始化加強器物件
        Enhancer enhancer = new Enhancer();
        //設定代理類
        enhancer.setSuperclass(operator.getClass());
        //設定代理回撥
        enhancer.setCallback(methodInterceptorImpl);

        //建立代理物件
        Operator operationProxy = (Operator) enhancer.create();
        //呼叫操作方法
        operationProxy.doSomething();
    }
}
複製程式碼

使用 CGLIB 進行動態代理的過程分為四個步驟:

  • 使用 MethodInterceptorImpl 實現 MethodInterceptor 介面,並在 intercept 方法中進行額外的操作
  • 建立增強器 Enhance 並設定被代理的操作類
  • 生成代理類
  • 呼叫代理物件的操作方法

總結

無論是靜態代理還是動態代理,都能一定程度地解決我們的問題,在開發過程中可以根據實際情況選擇合適的方案。總之,沒有好不好的方案,只有適不適合自己專案的方案,我們應該深入研究和理解方案背後的原理,以便能夠應對開發過程中產生的變數。

文章中的程式碼已經上傳至我的 Github,如果你對文章內容有不同意見,歡迎留言,我們一同探討。