Java 動態代理的多種實現方式
優勢:在不修改源碼的情況下,對目標方法進行相應的增強。
作用:完成程序功能之間的松耦合。
二、動態代理的多種實現 JDK代理:基于接口的動態代理技術(缺點,目標對象必須有接口,如果沒有接口,則無法完成動態代理的實現) cglib代理:基于父類的動態代理技術兩者的區別如圖所示:
目標接口類:
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(), //目標對象相同的接口字節碼對象數組new InvocationHandler() {//調用代理對象的任何方法,實質執行的都是invoke方法public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{advice.before();//前置增強Object invoke = method.invoke(target, args);//執行目標方法advice.after();//后置增強System.out.println();return invoke;}});//代理對象的方法測試proxyInstance.save();proxyInstance.print('JDK動態代理');}}
運行截圖:
需要導入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();//返回值就是動態生成的代理對象,基于cglib//創建增強器Enhancer enhancer = new Enhancer();//設置父類(目標)enhancer.setSuperclass(Target.class);//設置回調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;}});//創建代理對象Target proxy = (Target)enhancer.create();//測試代理方法proxy.save();proxy.print('基于cglib實現動態規劃');}}
運行截圖:
使用JDK動態代理實現時,最大限制是被增強對象必須實現接口,并且增強的方法只能是接口中聲明的方法。但在實際的項目中,可能總是存在對不實現業務接口的對象進行增強的需求,這時JDK動態代理將無能為力。
四、兩種方式的適用場景JDK動態代理優點 不依賴第三方jar包, 使用方便 隨著JDK的升級,JDK動態代理的性能在穩步提升缺點 只能代理實現了接口的類 執行速度較慢 適用場景 如果你的程序需要頻繁、反復地創建代理對象,則JDK動態代理在性能上更占優。cglib優點由于是動態生成字節碼實現代理,因此代理對象的執行速度較快, 約為JDK動態代理的1.5 ~ 2倍可以代理沒有實現接口的對象
缺點 不能代理final類 動態生成字節碼雖然執行較快,但是生成速度很慢,根據網上一些人的測試結果,cglib創建代理對象的速度要比JDK慢10 ~ 15倍。適用場景 不需要頻繁創建代理對象的應用,如Spring中默認的單例bean,只需要在容器啟動時生成一次代理對象。以上就是Java 動態代理的多種實現方式的詳細內容,更多關于Java 動態代理的實現的資料請關注好吧啦網其它相關文章!
相關文章: