Java 動態(tài)代理的多種實現(xiàn)方式
優(yōu)勢:在不修改源碼的情況下,對目標方法進行相應(yīng)的增強。
作用:完成程序功能之間的松耦合。
二、動態(tài)代理的多種實現(xiàn) JDK代理:基于接口的動態(tài)代理技術(shù)(缺點,目標對象必須有接口,如果沒有接口,則無法完成動態(tài)代理的實現(xiàn)) cglib代理:基于父類的動態(tài)代理技術(shù)兩者的區(qū)別如圖所示:
目標接口類:
public interface TargetInterface {public void save();public void print(String str);}
目標類:
public class Target implements TargetInterface{public void save() {System.out.println('save running...');}public void print(String str) {System.out.println(str);}}
增強類:
public class Advice {public void before() {System.out.println('前置增強');}public void after() {System.out.println('后置增強');}}
測試類:
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ProxyTest {public static void main(String[] args) {//目標對象final Target target = new Target();//增強對象final Advice advice = new Advice();TargetInterface proxyInstance = (TargetInterface)Proxy.newProxyInstance(target.getClass().getClassLoader(), //目標對象類加載器target.getClass().getInterfaces(), //目標對象相同的接口字節(jié)碼對象數(shù)組new InvocationHandler() {//調(diào)用代理對象的任何方法,實質(zhì)執(zhí)行的都是invoke方法public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{advice.before();//前置增強Object invoke = method.invoke(target, args);//執(zhí)行目標方法advice.after();//后置增強System.out.println();return invoke;}});//代理對象的方法測試proxyInstance.save();proxyInstance.print('JDK動態(tài)代理');}}
運行截圖:
需要導入Jar包,如果是maven項目,則在pom.xml文件加入如下配置:
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency>
目標類:
public class Target {public void save() {System.out.println('save running...');}public void print(String str) {System.out.println(str);}}
增強類:
public class Advice {public void before() {System.out.println('前置增強');}public void after() {System.out.println('后置增強');}}
測試類:
import java.lang.reflect.Method;import org.springframework.cglib.proxy.Enhancer;import org.springframework.cglib.proxy.MethodInterceptor;import org.springframework.cglib.proxy.MethodProxy;public class ProxyTest {public static void main(String[] args) {final Target target = new Target();final Advice advice = new Advice();//返回值就是動態(tài)生成的代理對象,基于cglib//創(chuàng)建增強器Enhancer enhancer = new Enhancer();//設(shè)置父類(目標)enhancer.setSuperclass(Target.class);//設(shè)置回調(diào)enhancer.setCallback(new MethodInterceptor() {public Object intercept(Object o, Method method, Object[] obj, MethodProxy methodProxy) throws Throwable{advice.before();Object invoke = method.invoke(target, obj);advice.after();System.out.println();return invoke;}});//創(chuàng)建代理對象Target proxy = (Target)enhancer.create();//測試代理方法proxy.save();proxy.print('基于cglib實現(xiàn)動態(tài)規(guī)劃');}}
運行截圖:
使用JDK動態(tài)代理實現(xiàn)時,最大限制是被增強對象必須實現(xiàn)接口,并且增強的方法只能是接口中聲明的方法。但在實際的項目中,可能總是存在對不實現(xiàn)業(yè)務(wù)接口的對象進行增強的需求,這時JDK動態(tài)代理將無能為力。
四、兩種方式的適用場景JDK動態(tài)代理優(yōu)點 不依賴第三方j(luò)ar包, 使用方便 隨著JDK的升級,JDK動態(tài)代理的性能在穩(wěn)步提升缺點 只能代理實現(xiàn)了接口的類 執(zhí)行速度較慢 適用場景 如果你的程序需要頻繁、反復地創(chuàng)建代理對象,則JDK動態(tài)代理在性能上更占優(yōu)。cglib優(yōu)點由于是動態(tài)生成字節(jié)碼實現(xiàn)代理,因此代理對象的執(zhí)行速度較快, 約為JDK動態(tài)代理的1.5 ~ 2倍可以代理沒有實現(xiàn)接口的對象
缺點 不能代理final類 動態(tài)生成字節(jié)碼雖然執(zhí)行較快,但是生成速度很慢,根據(jù)網(wǎng)上一些人的測試結(jié)果,cglib創(chuàng)建代理對象的速度要比JDK慢10 ~ 15倍。適用場景 不需要頻繁創(chuàng)建代理對象的應(yīng)用,如Spring中默認的單例bean,只需要在容器啟動時生成一次代理對象。以上就是Java 動態(tài)代理的多種實現(xiàn)方式的詳細內(nèi)容,更多關(guān)于Java 動態(tài)代理的實現(xiàn)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python如何換行輸出2. Python使用urlretrieve實現(xiàn)直接遠程下載圖片的示例代碼3. Python:UserWarning:此模式具有匹配組。要實際獲得組,請使用str.extract4. Android Studio中一套代碼多渠道打包的實現(xiàn)方法5. 詳解java google Thumbnails 圖片處理6. python如何計算圓的面積7. Java使用Tesseract-Ocr識別數(shù)字8. Android打包篇:Android Studio將代碼打包成jar包教程9. Java 接口和抽象類的區(qū)別詳解10. 解決Android Studio 格式化 Format代碼快捷鍵問題
