Java 動態(tài)代理的多種實(shí)現(xiàn)方式
優(yōu)勢:在不修改源碼的情況下,對目標(biāo)方法進(jìn)行相應(yīng)的增強(qiáng)。
作用:完成程序功能之間的松耦合。
二、動態(tài)代理的多種實(shí)現(xiàn) JDK代理:基于接口的動態(tài)代理技術(shù)(缺點(diǎn),目標(biāo)對象必須有接口,如果沒有接口,則無法完成動態(tài)代理的實(shí)現(xiàn)) cglib代理:基于父類的動態(tài)代理技術(shù)兩者的區(qū)別如圖所示:
目標(biāo)接口類:
public interface TargetInterface {public void save();public void print(String str);}
目標(biāo)類:
public class Target implements TargetInterface{public void save() {System.out.println('save running...');}public void print(String str) {System.out.println(str);}}
增強(qiáng)類:
public class Advice {public void before() {System.out.println('前置增強(qiáng)');}public void after() {System.out.println('后置增強(qiáng)');}}
測試類:
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ProxyTest {public static void main(String[] args) {//目標(biāo)對象final Target target = new Target();//增強(qiáng)對象final Advice advice = new Advice();TargetInterface proxyInstance = (TargetInterface)Proxy.newProxyInstance(target.getClass().getClassLoader(), //目標(biāo)對象類加載器target.getClass().getInterfaces(), //目標(biāo)對象相同的接口字節(jié)碼對象數(shù)組new InvocationHandler() {//調(diào)用代理對象的任何方法,實(shí)質(zhì)執(zhí)行的都是invoke方法public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{advice.before();//前置增強(qiáng)Object invoke = method.invoke(target, args);//執(zhí)行目標(biāo)方法advice.after();//后置增強(qiáng)System.out.println();return invoke;}});//代理對象的方法測試proxyInstance.save();proxyInstance.print('JDK動態(tài)代理');}}
運(yùn)行截圖:
需要導(dǎo)入Jar包,如果是maven項(xiàng)目,則在pom.xml文件加入如下配置:
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency>
目標(biāo)類:
public class Target {public void save() {System.out.println('save running...');}public void print(String str) {System.out.println(str);}}
增強(qiáng)類:
public class Advice {public void before() {System.out.println('前置增強(qiáng)');}public void after() {System.out.println('后置增強(qiáng)');}}
測試類:
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)建增強(qiáng)器Enhancer enhancer = new Enhancer();//設(shè)置父類(目標(biāo))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實(shí)現(xiàn)動態(tài)規(guī)劃');}}
運(yùn)行截圖:
使用JDK動態(tài)代理實(shí)現(xiàn)時,最大限制是被增強(qiáng)對象必須實(shí)現(xiàn)接口,并且增強(qiáng)的方法只能是接口中聲明的方法。但在實(shí)際的項(xiàng)目中,可能總是存在對不實(shí)現(xiàn)業(yè)務(wù)接口的對象進(jìn)行增強(qiáng)的需求,這時JDK動態(tài)代理將無能為力。
四、兩種方式的適用場景JDK動態(tài)代理優(yōu)點(diǎn) 不依賴第三方j(luò)ar包, 使用方便 隨著JDK的升級,JDK動態(tài)代理的性能在穩(wěn)步提升缺點(diǎn) 只能代理實(shí)現(xiàn)了接口的類 執(zhí)行速度較慢 適用場景 如果你的程序需要頻繁、反復(fù)地創(chuàng)建代理對象,則JDK動態(tài)代理在性能上更占優(yōu)。cglib優(yōu)點(diǎn)由于是動態(tài)生成字節(jié)碼實(shí)現(xiàn)代理,因此代理對象的執(zhí)行速度較快, 約為JDK動態(tài)代理的1.5 ~ 2倍可以代理沒有實(shí)現(xiàn)接口的對象
缺點(diǎn) 不能代理final類 動態(tài)生成字節(jié)碼雖然執(zhí)行較快,但是生成速度很慢,根據(jù)網(wǎng)上一些人的測試結(jié)果,cglib創(chuàng)建代理對象的速度要比JDK慢10 ~ 15倍。適用場景 不需要頻繁創(chuàng)建代理對象的應(yīng)用,如Spring中默認(rèn)的單例bean,只需要在容器啟動時生成一次代理對象。以上就是Java 動態(tài)代理的多種實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Java 動態(tài)代理的實(shí)現(xiàn)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. springcloud alibaba nacos linux配置的詳細(xì)教程2. Vue為什么要謹(jǐn)慎使用$attrs與$listeners3. SpringBoot 使用 @Value 注解讀取配置文件給靜態(tài)變量賦值4. Java 生成帶Logo和文字的二維碼5. SpringBoot整合MybatisPlus的教程詳解6. 解決在Vue中使用axios POST請求變成OPTIONS的問題7. Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法8. python 實(shí)現(xiàn)圖片修復(fù)(可用于去水印)9. Android自定義控件實(shí)現(xiàn)方向盤效果10. python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件
