国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring如何基于Proxy及cglib實現動態代理

瀏覽:22日期:2023-09-01 10:15:01

spring中提供了兩種動態代理的方式,分別是Java Proxy以及cglib

JavaProxy只能代理接口,而cglib是通過繼承的方式,實現對類的代理

添加一個接口以及對應的實現類

public interface HelloInterface { void sayHello();}

public class HelloInterfaceImpl implements HelloInterface { @Override public void sayHello() { System.out.println('hello'); }}

JavaProxy通過實現InvocationHandler實現代理

public class CustomInvocationHandler implements InvocationHandler { private HelloInterface helloInterface; public CustomInvocationHandler(HelloInterface helloInterface) { this.helloInterface = helloInterface; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println('before hello for proxy'); Object result = method.invoke(helloInterface, args); System.out.println('after hello for proxy'); return result; }}

而cglib實現MethodInterceptor進行方法上的代理

public class CustomMethodInterceptor implements MethodInterceptor { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println('before hello for cglib'); Object result = methodProxy.invokeSuper(o, objects); System.out.println('after hello for cglib'); return result; }}

分別實現調用代碼

public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(HelloInterfaceImpl.class); enhancer.setCallback(new CustomMethodInterceptor()); HelloInterface target = (HelloInterface) enhancer.create(); target.sayHello(); CustomInvocationHandler invocationHandler = new CustomInvocationHandler(new HelloInterfaceImpl()); HelloInterface target2 = (HelloInterface) Proxy.newProxyInstance(Demo.class.getClassLoader(), new Class[]{HelloInterface.class}, invocationHandler); target2.sayHello(); }

可以看到對于的代理信息輸出

before hello for cglibhelloafter hello for cglibbefore hello for proxyhelloafter hello for proxy

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本道色| 亚洲综合一 | 久久国产精品免费观看 | 日韩在线播放视频 | 在线亚洲成人 | 男人一进一出桶女人视频 | 不卡一区在线观看 | 香蕉久久一区二区不卡无毒影院 | 一区二区三区欧美 | 毛片手机在线视频免费观看 | 久久成年片色大黄全免费网站 | 99精品视频一区在线视频免费观看 | 欧美一级aⅴ毛片 | 久久久亚洲精品蜜桃臀 | 成人欧美精品一区二区不卡 | 久久99亚洲精品久久99 | 国产99视频在线观看 | 久污| 制服丝袜在线视频香蕉 | 国产三级日本三级日产三 | 亚洲视频免费一区 | 国产精品久久久久久久久99热 | 欧美精品日日鲁夜夜添 | 免费看一级做a爰片久久 | 韩国女主播青草在线观看 | 国产成人精品免费午夜 | 成 人 动漫在线观看网站网站 | 国产日韩久久 | 欧美另类高清xxxxx | 亚洲国产成人精品91久久久 | 成人a毛片 | 亚洲国产精久久久久久久春色 | 亚洲国产成人va在线观看网址 | 国内精品久久久久影院亚洲 | 国产成年视频 | 精品欧美小视频在线观看 | 欧美性毛片大片 | 在线观看国产精成人品 | aaaaaaa毛片| 免费国产视频在线观看 | 手机在线成人精品视频网 |