打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Nirvana Studio Blog Archive cglib 指南 :: 分享知...

cglib,全称是Code Generation Library,它可以用来动态继承Java类或者实现接口,很多知名的开源项目中用到了它,譬如Hibernate,Spring之类用它来实现动态代理。

增强一个已有类

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()
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
一个小例子演示 cglib 动态代理库
Java动态代理的两种实现方法
Spring的cglib代理!(给类生成子类)
java动态代理模式初解
秒懂Java代理与动态代理模式
面试官:说说你对Spring AOP 的实现机制的理解!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服