教你清楚了解JAVA動態代理

  • 代理在生活中很常見,比如說婚介網站,其實就是找對象的代理;還有社保代理、人事代理;還有找黃牛搶票,其實也是一種代理;而這些代理,在JAVA中也是有對應實現的。

1、為什麼要動態代理

動態代理的作用其實就是在不修改原代碼的前提下,對已有的方法進行增強。

關鍵點:

  • 不修改原來已有的代碼(滿足設計模式的要求)
  • 對已有方法進行增強

2、舉個栗子

我們用一個很簡單的例子來說明: Hello 類,有一個 introduction 方法。

現在我們的需求就是不修改 Hello 類的 introduction 方法,在 introduction 之前先 sayHello ,在 introduction 之後再 sayGoodBye

3、實現方式

JAVA中,實現動態代理有兩種方式,一種是JDK提供的,一種是第三方庫 CgLib 提供的。特點如下:

 
CgLib

3.1、JDK動態代理

JDK動態代理需要實現接口,然後通過對接口方法的增強來實現動態代理

所以要使用JDK動態代理的話,我們首先要創建一個接口,並且被代理的方法要在這個接口裡面

3.1.1、創建一個接口

我們創建一個接口如下:

Personal.java

 
public interface Personal { /** * 被代理的方法 */ void introduction(); }

3.1.2、實現接口

創建接口實現類,並且完成 introduction 方法

PersonalImpl.java

 
public class PersonalImpl implements Personal { @Override public void introduction() { System.out.println("我是程序員!"); } }

3.1.3、創建代理類

JDK代理的關鍵就是這個代理類了,需要實現 InvocationHandler

在代理類中,所有方法的調用都好分發到 invoke 方法中。我們在 invoke 方法完成對方法的增強即可

JDKProxyFactory.java

 
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class JDKProxyFactory implements InvocationHandler { /** * 目標對象 */ private T target; /** * 構造函數傳入目標對象 * * @param target 目標對象 */ public JDKProxyFactory(T target) { this.target = target; } /** * 獲取代理對象 * * @return 獲取代理 */ public T getProxy() { return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 對方法增強 System.out.println("大家好!"); // 調用原方法 Object result = method.invoke(target, args); // 方法增強 System.out.println("再見!"); return result; } }

就這樣,JDK動態代理的代碼就完成了,接下來寫一份測試代碼

3.1.4、編寫測試代碼

為了方便測試,我們編寫一個 test 方法

同時為了查看class文件,還添加了一個 generatorClass 方法,這個方法可以將動態代理生成的 .class 輸出到文件

ProxyTest.java

 
import org.junit.Test; import sun.misc.ProxyGenerator; import java.io.FileOutputStream; import java.io.IOException; public class ProxyTest { @Test public void testJdkProxy() { // 生成目標對象 Personal personal = new PersonalImpl(); // 獲取代理對象 JDKProxyFactory proxyFactory = new JDKProxyFactory<>(personal); Personal proxy = proxyFactory.getProxy(); // 將proxy的class字節碼輸出到文件 generatorClass(proxy); // 調用代理對象 proxy.introduction(); } /** * 將對象的class字節碼輸出到文件 * * @param proxy 代理類 */ private void generatorClass(Object proxy) { FileOutputStream out = null; try { byte[] generateProxyClass = ProxyGenerator.generateProxyClass(proxy.getClass().getSimpleName(), new Class[]{proxy.getClass()}); out = new FileOutputStream(proxy.getClass().getSimpleName() + ".class"); out.write(generateProxyClass); } catch (Exception e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block } } } } }

3.1.5、查看運行結果

可以看到,運行 test 方法之後,控制檯打印出如下:

 
大家好! 我是程序員! 再見!

我們在 introduction 方法前和後都成功增加了功能,讓這個程序員的自我介紹瞬間變得更加有禮貌了。

3.1.6、探探動態代理的秘密

動態代理的代碼並不多,那麼JDK底層是怎麼幫我們實現的呢?

在測試的時候我們將動態生成的代理類的 class 字節碼輸出到了文件,我們可以反編譯看看。

結果有點長,就不全部貼出來了,不過我們可以看到,裡面有一個 introduction 方法如下:

 
/** * the invocation handler for this proxy instance. * @serial */ protected InvocationHandler h; protected Proxy(InvocationHandler h) { Objects.requireNonNull(h); this.h = h; } public final void introduction() throws { try { super.h.invoke(this, m3, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } }

原來,生成的代理對象裡面,引用了我們的 InvocationHandler ,然後在將 introduction 方法裡面調用了 InvocationHandler 的 introduction ,而 InvocationHandler 是由我們編寫的代理類,在這裡我們增加了 sayHello 和 sayGoodBye 操作,然後還調用了原對象的 introduction 方法,就這樣完成了動態代理。

3.2、CgLib動態代理

CgLib 動態

3.2.1、創建被代理對象

由於 CgLib 不需要實現接口,所以我們不需要創建接口文件了(當然,你要有接口也沒有問題)

直接創建目標類,實現 introduction 方法

PersonalImpl.java

 
public class PersonalImpl { public void introduction() { System.out.println("我是程序員!"); } }

3.2.2、創建代理類

同樣,我們也需要創建代理類,並且在這裡實現增強的邏輯,這次我們不是實現 InvocationHandler 接口了,而是實現 CgLib 提供的接口 MethodInterceptor ,都是類似的, MethodInterceptor 中,全部方法調用都會交給 intercept 處理,我們在 intercept 添加處理邏輯即可。

CgLibProxyFactory.java

 
import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CgLibProxyFactory implements MethodInterceptor { /** * 獲取代理對象 * * @param tClass 被代理的目標對象 * @return 代理對象 */ public T getProxyByCgLib(Class tClass) { // 創建增強器 Enhancer enhancer = new Enhancer(); // 設置需要增強的類的類對象 enhancer.setSuperclass(tClass); // 設置回調函數 enhancer.setCallback(this); // 獲取增強之後的代理對象 return (T) enhancer.create(); } /** * 代理類方法調用回調 * * @param obj 這是代理對象,也就是[目標對象]的子類 * @param method [目標對象]的方法 * @param args 參數 * @param proxy 代理對象的方法 * @return 返回結果,返回給調用者 * @throws Throwable */ @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("大家好!"); Object result = proxy.invokeSuper(obj, args); System.out.println("再見!"); return result; } }

3.2.3、編寫測試代碼

在剛才的測試方法中,我們添加一個 cglib 的測試方法:

 
@Test public void testCgLibProxy() { // 生成被代理的目標對象 PersonalImpl personal = new PersonalImpl(); // 獲取代理類 CgLibProxyFactory proxyFactory = new CgLibProxyFactory<>(); PersonalImpl proxy = proxyFactory.getProxyByCgLib((Class) personal.getClass()); // 將proxy的class字節碼輸出到文件 generatorClass(proxy); // 調用代理對象 proxy.introduction(); }

3.2.4、查看運行結果

運行測試用例,可以看到跟JDK的實現一樣的效果

 
大家好! 我是程序員! 再見!

3.2.5、探探動態代理的秘密

跟JDK的測試一樣,我們也來看看生成的 class 文件

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

可以發現,與JDK的動態代理並沒有區別。

4、如何選擇

既然有兩種實現方式,那麼到底應該怎麼選擇呢?

就兩個原則:

  • 目標類有接口實現的, JDK 和 CgLib 都可以選擇,你開心就好
  • 目標類沒有實現任何接口,那隻能用 CgLib 了

分享一些知識點給大家希望能幫助到大家,或者從中啟發。


分享到:


相關文章: