一、反射
概念:在運行狀態中,對于任意的一個類,都能夠知道這個類的所有字段和方法,對任意一個對象都能夠通過反射機制調用一個類的任意方法
實現方法:JVM在第一次加載某個類時會生成一個Class對象,里面記錄了這個類的信息
鏈接:類加載機制(留坑)
二、動態代理
動態代理的作用:在不改變原代碼的基礎上增加新的功能,如日志、權限檢驗等
反射在動態代理中的應用:由于知道原類的字段、方法等信息,才可以通過代理類執行被代理類的方法
動態代理的實現有兩種
1、JDK代理
實現方法:通過創建一個代理類,這個代理類繼承于一個Proxy類,Proxy類中有一個InvocationHandler接口,這個接口持有被代理對象和一個invoke()方法。創建好代理類對象后,對該對象調用的方法都會交由invoke方法處理。invoke方法接受3個參數:代理對象、方法、參數列表。重寫invoke方法便可以在原方法的基礎上添加其他邏輯
一個JDK代理的簡單實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; // 定義一個接口 interface MyInterface { void fun(); } // 用一個類去實現這個接口 class Person implements MyInterface { @Override public void fun() { System.out.println( "Person實現接口方法" ); } } // 用一個類實現InvocationHandler接口 class MyInvocationHandler<T> implements InvocationHandler { //InvocationHandler持有的被代理對象 T target; public MyInvocationHandler(T target) { this .target = target; } /** * proxy:代表動態代理對象 * method:代表正在執行的方法 * args:代表調用目標方法時傳入的實參 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println( "代理執行" + method.getName() + "方法" ); // 在原方法的基礎上添加其他邏輯 Object result = method.invoke(target, args); // 通過invoke方法調用原方法 return result; } } public class Main { public static void main(String[] args) { Person person = new Person(); InvocationHandler invocationHandler = new MyInvocationHandler<>(person); MyInterface proxy = (MyInterface) Proxy.newProxyInstance(Person. class .getClassLoader(), Person. class .getInterfaces(), invocationHandler); proxy.fun(); } } |
輸出結果:
代理執行fun方法
Person實現接口方法
缺陷:只能代理接口方法,因為JDK代理需要繼承一個Proxy類,又由于Java的單繼承機制,導致代理類無法繼承父類的函數,只能實現接口
2、CGLIB代理
原理與JDK代理類似,區別在于CGLIB代理創建的代理類直接繼承于被代理類,所以可以實現被代理類的方法而非僅僅接口方法
一個簡單的CGLIB代理實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Car. class ); enhancer.setCallback( new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println( "before" ); Object res = methodProxy.invokeSuper(obj, args); System.out.println( "after" ); return res; } }); Car car = (Car) enhancer.create(); car.print(); } } class Car { void print() { System.out.println( "執行原方法" ); } } |
由于CGLIB并非JDK自帶,所以需要通過Maven引入一個依賴
1
2
3
4
5
|
< dependency > < groupId >org.sonatype.sisu.inject</ groupId > < artifactId >cglib</ artifactId > < version >3.1.1</ version > </ dependency > |
輸出結果:
before
執行原方法
after
3、JDK代理與CGLIB代理對比
1、JDK代理只能實現接口方法,而CGLIB代理既可以實現接口方法也可以實現類中自帶的方法
2、性能上,在JDK1.8,CGLIB3.1.1的環境上,每次創建一個代理類并執行同樣的方法
當執行10000次,JDK代理用時85ms,而CGLIB代理用時190ms,明顯JDK代理性能更佳;
當執行1000000(一百萬)次時,兩種代理耗時幾乎相等;
當執行10000000次時,CGLIB代理已經優于JDK代理。
所以在執行次數少時,JDK代理性能更好;反之CGLIB代理性能更好(但是重復執行多于1000000次的任務幾乎沒有吧
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/qq_45404693/article/details/119972532