詳解java JDK 動態(tài)代理類分析(java.lang.reflect.Proxy)
更新時間:2017年06月08日 17:26:57 作者:Alex_zhuang
這篇文章主要介紹了詳解java JDK 動態(tài)代理類分析(java.lang.reflect.Proxy)的相關(guān)資料,需要的朋友可以參考下
詳解java JDK 動態(tài)代理類分析(java.lang.reflect.Proxy)
/** * JDK 動態(tài)代理類分析(java.lang.reflect.Proxy使用) * * @author 張明學(xué) * */ public class ProxyStudy { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { // 動態(tài)代理類:通用指定類加載器,和接口產(chǎn)生一類 // getProxyClass()返回代理類的 java.lang.Class 對象,并向其提供類加載器和接口數(shù)組。 Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class); System.out.println("動態(tài)產(chǎn)生的類名為:" + clazzProxy.getName()); System.out.println("----------獲取動態(tài)產(chǎn)生的類的構(gòu)造方法---------"); Constructor[] constructors = clazzProxy.getConstructors(); int i = 1; for (Constructor constructor : constructors) { System.out.println("第" + (i++) + "個構(gòu)造方法名:" + constructor.getName()); Class[] parameterClazz = constructor.getParameterTypes(); System.out.println("第" + (i++) + "個構(gòu)造方法參數(shù):" + Arrays.asList(parameterClazz)); } System.out.println("----------獲取動態(tài)產(chǎn)生的類的普通方法---------"); Method[] methods = clazzProxy.getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; System.out.println("第" + (j + 1) + "個普通方法名:" + method.getName()); Class[] parameterClazz = method.getParameterTypes(); System.out.println("第" + (j + 1) + "個普通方法參數(shù):" + Arrays.asList(parameterClazz)); } System.out.println("---------獲取動態(tài)代理對象的構(gòu)造方法---------"); // 動態(tài)代理產(chǎn)生的對象的構(gòu)造方法需要一個實(shí)現(xiàn)java.lang.reflect.InvocationHandler接口的對象,故不能通過 // clazzProxy.newInstance();產(chǎn)生一個對象,可以根據(jù)構(gòu)造方法產(chǎn)生一個對象 // InvocationHandler 是代理實(shí)例的調(diào)用處理程序 實(shí)現(xiàn)的接口。 Constructor constructor = clazzProxy.getConstructor(InvocationHandler.class); // 代理產(chǎn)生的對象 Collection proxyBuildCollection = (Collection) constructor .newInstance(new InvocationHandler() { // 為什么這里選擇ArrayList作為目標(biāo)對象? // 因為這里的constructor是clazzProxy這個動態(tài)類的構(gòu)造方法,clazzProxy是通過Proxy.getProxyClass()方法產(chǎn)生的, // 該方法有兩個參數(shù),一個是指定類加載器,一個是指定代理要實(shí)現(xiàn)的接口,這個接口我上面指定了Collection // 而ArrayList實(shí)現(xiàn)了Collection接口,固可以為該動態(tài)類的目標(biāo)對象 ArrayList target = new ArrayList();// 動態(tài)類的目標(biāo)對象 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("執(zhí)行目標(biāo)" + method.getName() + "方法之前:" + System.currentTimeMillis()); Object result = method.invoke(target, args);// 其實(shí)代理對象的方法調(diào)用還是目標(biāo)對象的方法 System.out.println("執(zhí)行目標(biāo)" + method.getName() + "方法之后:" + System.currentTimeMillis()); return result; } }); proxyBuildCollection.clear(); proxyBuildCollection.add("abc"); proxyBuildCollection.add("dbc"); System.out.println(proxyBuildCollection.size()); System.out.println(proxyBuildCollection.getClass().getName()); /** * 動態(tài)代理:總結(jié)如下: * 1,通過Proxy.getProxyClass(classLoader,interface)方法產(chǎn)生一個動態(tài)類的class字節(jié)碼(clazz) * 該getProxyClass()方法有兩個參數(shù):一個是指定該動態(tài)類的類加載器,一個是該動態(tài)類的要實(shí)現(xiàn)的接口(從這里可以看現(xiàn)JDK的動態(tài)代理必須要實(shí)現(xiàn)一個接口) * * 2,通過第一步的獲取的clazz對象可以獲取它的構(gòu)造方法constructor,那么就可以通用constructor的newInstance()方法構(gòu)造出一個動態(tài)實(shí)體對象 * 但constructor的newInstance()方法需要指定一個實(shí)現(xiàn)了InvocationHandler接口的類handler,在該類中需要一個目標(biāo)對象A和實(shí)現(xiàn)invoke方法 * 目標(biāo)對象A要求能對第一步中的接口的實(shí)現(xiàn),因為在invoke方法中將會去調(diào)用A中的方法并返回結(jié)果。 * 過程如下:調(diào)用動態(tài)代理對象ProxyObject的x方法 ————> 進(jìn)入構(gòu)造方法傳進(jìn)的handler的invoke方法 ————> invoke方法調(diào)用handler中的target對象 * 的x方法(所以要求target必須要實(shí)現(xiàn)構(gòu)造動態(tài)代理類時指定的接口)并返回它的返回值。(其實(shí)如果我們代理P類,那么target就可以選中P類,只是要求P必需實(shí)現(xiàn)一個接口) * * 那么上述中x方法有哪些呢?除了從Object繼承過來的方法中除toString,hashCode,equals外的方法不交給handler外,其它的方法全部交給handler處理 * 如上面proxyBuildCollection.getClass().getName()就沒有調(diào)用handler的getClass方法,而是調(diào)用自己的 * * 3,在handler的invoke方法中return method.invoke(target,args)就是將方法交給target去完成。那么在這個方法執(zhí)行之前,之后,異常時我們都可以做一些操作, * 并且可以在執(zhí)行之前檢查方法的參數(shù)args,執(zhí)行之后檢查方法的結(jié)果 */ System.out.println("-------------------下面的寫法更簡便--------------------"); // proxyBuildColl是對ArrayList進(jìn)行代理 Collection proxyBuildCollection2 = (Collection) Proxy.newProxyInstance( Collection.class.getClassLoader(),// 指定類加載器 new Class[] { Collection.class },// 指定目標(biāo)對象實(shí)現(xiàn)的接口 // 指定handler new InvocationHandler() { ArrayList target = new ArrayList(); public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println(method.getName() + "執(zhí)行之前..."); if (null != args) { System.out.println("方法的參數(shù):" + Arrays.asList(args)); } else { System.out.println("方法的參數(shù):" + null); } Object result = method.invoke(target, args); System.out.println(method.getName() + "執(zhí)行之后..."); return result; } }); proxyBuildCollection2.add("abc"); proxyBuildCollection2.size(); proxyBuildCollection2.clear(); proxyBuildCollection2.getClass().getName(); System.out.println("-------------------對JDK動態(tài)代理的重構(gòu)--------------------"); Set proxySet = (Set) buildProxy(new HashSet(), new MyAdvice()); proxySet.add("abc"); proxySet.size(); } /** * 構(gòu)造一個目標(biāo)對象的代理對象 * * @param target * 目標(biāo)對象(需要實(shí)現(xiàn)某個接口) * @return */ public static Object buildProxy(final Object target,final AdviceInter advice) { Object proxyObject = Proxy.newProxyInstance( target.getClass().getClassLoader(),// 指定類加載器 target.getClass().getInterfaces(), // 指定目標(biāo)對象實(shí)現(xiàn)的接口 // handler new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { advice.beforeMethod(target, method, args); Object result = method.invoke(target, args); advice.afterMethod(target, method, args); return result; } }); return proxyObject; } }
/** * 代理中執(zhí)行目標(biāo)方法之前之后的操作的一個實(shí)例 * * @author 張明學(xué) * */ public class MyAdvice implements AdviceInter { public void afterMethod(Object target, Method method, Object[] args) { System.out.println("目標(biāo)對象為:" + target.getClass().getName()); System.out.println(method.getName() + "執(zhí)行完畢!"); } public void beforeMethod(Object target, Method method, Object[] args) { System.out.println(method.getName() + "開始執(zhí)行"); if (null != args) { System.out.println("參數(shù)為:" + Arrays.asList(args)); } else { System.out.println("參數(shù)為:" + null); } } }
/** * 代理中執(zhí)行目標(biāo)方法之前之后的操作 * * @author 張明學(xué) * */ public interface AdviceInter { /** * 目標(biāo)方法執(zhí)行之前 * */ public void beforeMethod(Object target, Method method, Object[] args); /** * 目標(biāo)方法執(zhí)行之后 * * @param target * 目標(biāo)對象 * @param method * 方法 * @param args * 參數(shù) */ public void afterMethod(Object target, Method method, Object[] args); }
相關(guān)文章
SpringBoot全局配置long轉(zhuǎn)String丟失精度的問題解決
web項目中,Java后端傳過來的Long/long類型,前端JS接收會丟失精度。那么應(yīng)該如何解決,本文就來介紹一下幾種方法,感興趣的可以了解一下2021-08-08Java面試之動態(tài)規(guī)劃與組合數(shù)
這篇文章主要介紹了Java面試之動態(tài)規(guī)劃與組合數(shù)的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09SpringBoot?RESTful?應(yīng)用中的異常處理梳理小結(jié)
這篇文章主要介紹了SpringBoot?RESTful?應(yīng)用中的異常處理梳理小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05MyBatis與SpringMVC相結(jié)合實(shí)現(xiàn)文件上傳、下載功能
這篇文章主要介紹了MyBatis與SpringMVC相結(jié)合實(shí)現(xiàn)文件上傳、下載功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06Java Spring Boot消息服務(wù)萬字詳解分析
在實(shí)際項目開發(fā)中,有時需要與其他系統(tǒng)進(jìn)行集成完成相關(guān)業(yè)務(wù)功能,這種情況最原始做法是程序內(nèi)部相互調(diào)用,除此之外,還可以用消息服務(wù)中間件進(jìn)行業(yè)務(wù)處理,用消息服務(wù)中間件處理業(yè)務(wù)能夠提升系統(tǒng)的異步通信和擴(kuò)展解耦能力。Spring Boot對消息服務(wù)管理提供了非常好的支持2021-10-10