詳細(xì)分析java 動(dòng)態(tài)代理
1、動(dòng)態(tài)代理的特點(diǎn):
字節(jié)碼隨用隨創(chuàng)建,隨用隨加載
2、作用:
不修改源碼的基礎(chǔ)上對(duì)源碼進(jìn)行加強(qiáng)
3、分類(lèi):
(1)基于接口的動(dòng)態(tài)代理:
涉及到的類(lèi):Proxy,由JDK官方提供,使用Proxy類(lèi)中的newProxyInstance方法創(chuàng)建對(duì)象。創(chuàng)建代理對(duì)象時(shí)要求被代理對(duì)象至少實(shí)現(xiàn)一個(gè)接口,否則無(wú)法使用
參數(shù):
ClassLoader:類(lèi)加載器,他是用于加載對(duì)象字節(jié)碼的,和被代理對(duì)象使用相同的類(lèi)加載器,為固定寫(xiě)法 class[]:字節(jié)碼數(shù)組,他是用于讓代理對(duì)象和被代理對(duì)象具有相同的方法,也是固定寫(xiě)法 InvocationHandler:用戶(hù)提供增強(qiáng)的代碼 ,他是讓我們寫(xiě)如何代理。我們一般都是寫(xiě)一個(gè)該接口的實(shí)現(xiàn)類(lèi),通常情況下都是匿名內(nèi)部類(lèi),但不是必須的,此接口的實(shí)現(xiàn)類(lèi)都是誰(shuí)用誰(shuí)寫(xiě)示例: 創(chuàng)建 Producter接口和實(shí)體類(lèi)
package com.mingqi.proxy;/** * 對(duì)生產(chǎn)廠(chǎng)家要求的接口 */public interface IProducer { /** * 銷(xiāo)售 * @param money */ public void SaleProduct(float money); /** * 售后 * @param money */ public void AfterService(float money);}
package com.mingqi.proxy;public class Producer implements IProducer { public void SaleProduct(float money) { System.out.println('銷(xiāo)售產(chǎn)品,并拿到錢(qián):'+money); } public void AfterService(float money) { System.out.println('提供售后服務(wù),并拿到錢(qián):'+money); }}
測(cè)試方法:
public static void main(String[] args) { /* 1、動(dòng)態(tài)代理 特點(diǎn):字節(jié)碼隨用隨創(chuàng)建,隨用隨加載 作用:不修改源碼的基礎(chǔ)上對(duì)源碼進(jìn)行加強(qiáng) 分類(lèi):基于接口的動(dòng)態(tài)代理 涉及的類(lèi): Proxy 提供者:JDK官方 如何創(chuàng)建代理對(duì)象: 使用Proxy類(lèi)中的newProxyInstance方法 創(chuàng)建代理對(duì)象的要求: 被代理對(duì)象至少實(shí)現(xiàn)一個(gè)接口,如果沒(méi)有則不能使用 newProxyInstance的方法參數(shù): ClassLoader:類(lèi)加載器 他是用于加載代理對(duì)象字節(jié)碼的,和被代理對(duì)象使用相同的類(lèi)加載器,固定寫(xiě)法 class[] :字節(jié)碼數(shù)組 InvocationHandler 用于提供增強(qiáng)的代碼 他是讓我們寫(xiě)如何代理,我們一般都是寫(xiě)一個(gè)接口的實(shí)現(xiàn)類(lèi),通常情況下都是匿名內(nèi)部類(lèi),但不是必須的,此接口的實(shí)現(xiàn)類(lèi)都是誰(shuí)用誰(shuí)寫(xiě)*/ final Producer producer=new Producer(); IProdurcer proxyProducer= (IProdurcer)Proxy.newProxyInstance(producer.getClass().getClassLoader(), producer.getClass().getInterfaces(), new InvocationHandler() { /** * 作用:執(zhí)行被代理對(duì)象的任何接口方法都會(huì)經(jīng)過(guò)該方法 * 方法參數(shù)的含義 * @param proxy 代理對(duì)象的引用 * @param method 當(dāng)前執(zhí)行的方法 * @param args 當(dāng)前執(zhí)行方法所需的參數(shù) * @return 被代理對(duì)象有相同的返回值 * @throws Throwable */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //提供增強(qiáng)的代碼: Object returnValue=null; //1、獲取方法執(zhí)行的參數(shù) Float money=(Float) args[0]; //判斷當(dāng)前方法是否是銷(xiāo)售 if('SaleProduct'.equals(method.getName())) { returnValue=method.invoke(producer,money*0.8f); } return returnValue; } }); proxyProducer.SaleProduct(10000f); }
(2) 基于子類(lèi)的動(dòng)態(tài)代理
涉及到的類(lèi):Enhancer,由第三方cglib提供,使用Enhancer類(lèi)中的create方法創(chuàng)建對(duì)象。創(chuàng)建代理對(duì)象的類(lèi)不能是最終類(lèi),否則無(wú)法使用
參數(shù):
Class:字節(jié)碼,他是用于指定被代理對(duì)象的字節(jié)碼,為固定寫(xiě)法 Callback:用戶(hù)提供增強(qiáng)的代碼 ,他是讓我們寫(xiě)如何代理。我們一般都是寫(xiě)一個(gè)該接口的實(shí)現(xiàn)類(lèi),通常情況下都是匿名內(nèi)部類(lèi),但不是必須的,此接口的實(shí)現(xiàn)類(lèi)都是誰(shuí)用誰(shuí)寫(xiě),我們一般寫(xiě)的都是該接口的子接口實(shí)現(xiàn)類(lèi)MethodInterceptor示例: 創(chuàng)建 Product接口和實(shí)體類(lèi)
package com.mingqi.cglib;/** * 一個(gè)生產(chǎn)者 */public class Product { /** * 銷(xiāo)售 * @param money */ public void saleProduct(float money){ System.out.println('銷(xiāo)售產(chǎn)品,并拿到錢(qián):'+money); } /** * 售后 * @param money */ public void afterService(float money){ System.out.println('提供售后服務(wù),并拿到錢(qián):'+money); }}
測(cè)試類(lèi)及方法:
package com.mingqi.cglib;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 Client { public static void main(String[] args) { final Product product=new Product(); Product cglibproduct= (Product) Enhancer.create(product.getClass(), new MethodInterceptor() { public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {Object returnValue=null;//1、獲取方法執(zhí)行的參數(shù)Float money=(Float)objects[0];//判斷當(dāng)前方法是不是銷(xiāo)售if('SaleProduct'.equals(method.getName())) { returnValue = method.invoke(product, money*0.8f);}return returnValue; } }); cglibproduct.SaleProduct(1000f); }}
以上就是創(chuàng)建動(dòng)態(tài)代理對(duì)象的兩種類(lèi)型,以后要經(jīng)常練習(xí)使用,讓這種思想能給我們工作中帶來(lái)方便。
到此這篇關(guān)于詳細(xì)分析java 動(dòng)態(tài)代理的文章就介紹到這了,更多相關(guān)java 動(dòng)態(tài)代理內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. vue+vuex+axios從后臺(tái)獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作2. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)3. springboot項(xiàng)目整合druid數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)4. Python 忽略文件名編碼的方法5. android 控件同時(shí)監(jiān)聽(tīng)單擊和雙擊實(shí)例6. JavaScript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能7. SpringBoot使用Captcha生成驗(yàn)證碼8. 解決vue頁(yè)面刷新,數(shù)據(jù)丟失的問(wèn)題9. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作10. python logging.info在終端沒(méi)輸出的解決
