实战CGLib系列之proxy篇(一):方法拦截MethodInterceptor

实战CGLib系列文章java

本篇介绍经过MethodInterceptor和Enhancer实现一个动态代理。编程

1、首先说一下JDK中的动态代理框架

JDK中的动态代理是经过反射类Proxy以及InvocationHandler回调接口实现的,不了解的同窗请参考个人这篇Blog:Java动态代理详解 http://shensy.iteye.com/blog/1698197 spa

可是,JDK中所要进行动态代理的类必需要实现一个接口,也就是说只能对该类所实现接口中定义的方法进行代理,这在实际编程中具备必定的局限性,并且使用反射的效率也并非很高。代理

2、使用CGLib实现orm

使用CGLib实现动态代理,彻底不受代理类必须实现接口的限制,并且CGLib底层采用ASM字节码生成框架,使用字节码技术生成代理类,比使用Java反射效率要高。惟一须要注意的是,CGLib不能对声明为final的方法进行代理,由于CGLib原理是动态生成被代理类的子类。blog

下面,将经过一个实例介绍使用CGLib实现动态代理。接口

一、被代理类get

首先,定义一个类,该类没有实现任何接口,包含两个方法。it

Java代码  

  1. public class ConcreteClassNoInterface {  

  2.     public String getConcreteMethodA(String str){  

  3.         System.out.println("ConcreteMethod A ... "+str);  

  4.         return str;  

  5.     }  

  6.     public int getConcreteMethodB(int n){  

  7.         System.out.println("ConcreteMethod B ... "+n);  

  8.         return n+10;  

  9.     }  

  10. }  

二、拦截器

定义一个拦截器。在调用目标方法时,CGLib会回调MethodInterceptor接口方法拦截,来实现你本身的代理逻辑,相似于JDK中的InvocationHandler接口。

Java代码  

  1. public class ConcreteClassInterceptor implements MethodInterceptor{  

  2.     public Object intercept(Object obj, Method method, Object[] arg, MethodProxy proxy) throws Throwable {  

  3.         System.out.println("Before:"+method);    

  4.         Object object=proxy.invokeSuper(obj, arg);  

  5.         System.out.println("After:"+method);   

  6.         return object;  

  7.     }  

  8. }  

参数:Object为由CGLib动态生成的代理类实例,Method为上文中实体类所调用的被代理的方法引用,Object[]为参数值列表,MethodProxy为生成的代理类对方法的代理引用。

返回:从代理实例的方法调用返回的值。

其中,proxy.invokeSuper(obj,arg):

调用代理类实例上的proxy方法的父类方法(即实体类ConcreteClassNoInterface中对应的方法)

在这个示例中,只在调用被代理类方法先后各打印了一句话,固然实际编程中能够是其它复杂逻辑。

三、生成动态代理类

Java代码  

  1. Enhancer enhancer=new Enhancer();  

  2. enhancer.setSuperclass(ConcreteClassNoInterface.class);  

  3. enhancer.setCallback(new ConcreteClassInterceptor());  

  4. ConcreteClassNoInterface ccni=(ConcreteClassNoInterface)enhancer.create();  

这里Enhancer类是CGLib中的一个字节码加强器,它能够方便的对你想要处理的类进行扩展,之后会常常看到它。

首先将被代理类ConcreteClassNoInterface设置成父类,而后设置拦截器ConcreteClassInterceptor,最后执行enhancer.create()动态生成一个代理类,并从Object强制转型成父类型ConcreteClassNoInterface。

最后,在代理类上调用方法:

Java代码  

  1. ccni.getConcreteMethodA("shensy");  

  2. ccni.getConcreteMethodB(0);  

查看控制台输出:

控制台代码  

  1. Before :public java.lang.String generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodA(java.lang.String)  

  2. ConcreteMethod A ... shensy  

  3. After :public java.lang.String generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodA(java.lang.String)  

  4. Before :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)  

  5. ConcreteMethod B ... 0  

  6. After :public int generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB(int)  

能够看到,拦截器在调用被代理类方法先后都执行了print操做。

相关文章
相关标签/搜索