欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Nirvana Studio Blog Archive cglib 指南 :: 分享知...

cglib,全稱(chēng)是Code Generation Library,它可以用來(lái)動(dòng)態(tài)繼承Java類(lèi)或者實(shí)現接口,很多知名的開(kāi)源項目中用到了它,譬如Hibernate,Spring之類(lèi)用它來(lái)實(shí)現動(dòng)態(tài)代理。

增強一個(gè)已有類(lèi)

public class MyClass { public void method() {System.out.println("MyClass.method()");}}
import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodProxy;import net.sf.cglib.proxy.MethodInterceptor; public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(MyClass.class);enhancer.setCallback( new MethodInterceptorImpl() );  MyClass my = (MyClass)enhancer.create(); my.method();} private static class MethodInterceptorImpl implements MethodInterceptor {public Object intercept(Object obj,Method method,Object[] args,MethodProxy proxy) throws Throwable { System.out.println(method); proxy.invokeSuper(obj, args)return null;}}}

執行結果:

public void cglib_test.MyClass.method()MyClass.method()

使用CallbackFilter

public class MyClass { public void method() {System.out.println("MyClass.method()");} public void method2() {System.out.println("MyClass.method2()");}}
import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodProxy;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.NoOp;import net.sf.cglib.proxy.Callback;import net.sf.cglib.proxy.CallbackFilter;  public class Main { public static void main(String[] args) { Callback[] callbacks =new Callback[] { new MethodInterceptorImpl(),  NoOp.INSTANCE }; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(MyClass.class);enhancer.setCallbacks( callbacks );enhancer.setCallbackFilter( new CallbackFilterImpl() );  MyClass my = (MyClass)enhancer.create(); my.method();my.method2();} private static class CallbackFilterImpl implements CallbackFilter { public int accept(Method method) { if ( method.getName().equals("method2") ) {return 1} else {return 0;}}} private static class MethodInterceptorImpl implements MethodInterceptor {public Object intercept(Object obj,Method method,Object[] args,MethodProxy proxy) throws Throwable { System.out.println(method)return proxy.invokeSuper(obj, args);}}}

執行結果:

public void cglib_test.MyClass.method()MyClass.method()MyClass.method2()

使用Mixin

public interface MyInterfaceA { public void methodA();} public interface  MyInterfaceB {public void methodB();} public class MyInterfaceAImpl implements MyInterfaceA { public void methodA() {System.out.println("MyInterfaceAImpl.methodA()");}} public class MyInterfaceBImpl implements MyInterfaceB { public void methodB() {System.out.println("MyInterfaceBImpl.methodB()");}}
import net.sf.cglib.proxy.Mixin; public class Main { public static void main(String[] args) { Class[] interfaces =new Class[] { MyInterfaceA.class, MyInterfaceB.class };Object[] delegates =new Object[] { new MyInterfaceAImpl(), new MyInterfaceBImpl() };Object obj = Mixin.create(interfaces, delegates);  MyInterfaceA myA = (MyInterfaceA)obj;myA.methodA();  MyInterfaceB myB = (MyInterfaceB)obj;myB.methodB();}}
執行結果:
MyInterfaceAImpl.methodA()MyInterfaceBImpl.methodB()
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
一個(gè)小例子演示 cglib 動(dòng)態(tài)代理庫
Java動(dòng)態(tài)代理的兩種實(shí)現方法
Spring的cglib代理?。ńo類(lèi)生成子類(lèi))
java動(dòng)態(tài)代理模式初解
秒懂Java代理與動(dòng)態(tài)代理模式
面試官:說(shuō)說(shuō)你對Spring AOP 的實(shí)現機制的理解!
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久