java提供的基于Proxy、InvocationHandler的动态代理由于只能创建接口的代理对象,在实际应用中不能很好的满足需要,针对这个问题,另外一个开源框架cglib提供了一种实现方式,可以针对类进行代理对象的创建。
cglib官方网站:http://sourceforge.net/projects/cglib/files/
另外cglib依赖于asm(http://asm.ow2.org/)。
cglib使用示例:
HelloWorld.java
- public class HelloWorld{
- public String sayHello(String name){
- System.out.println("执行sayHello()");
- return "hello,"+name;
- }
- }
public class HelloWorld{ public String sayHello(String name){ System.out.println("执行sayHello()"); return "hello,"+name; } }
拦截器MyMethodInterceptor.java
- import java.lang.reflect.Method;
- import net.sf.cglib.proxy.MethodInterceptor;
- import net.sf.cglib.proxy.MethodProxy;
- public class MyMethodInterceptor implements MethodInterceptor{
- /*
- * obj 代理对象实例
- * method 源对象的方法名
- * args 传递给方法的实际入参
- * proxyMethod 与源对象中的method相对应的代理对象中的方法
- */
- public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
- System.out.println("执行方法"+method+"前");
- //执行源对象的method方法
- Object returnValue = proxy.invokeSuper(obj, args);
- System.out.println("执行方法"+method+"后");
- return returnValue;
- }
- }
import java.lang.reflect.Method; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; public class MyMethodInterceptor implements MethodInterceptor{ /* * obj 代理对象实例 * method 源对象的方法名 * args 传递给方法的实际入参 * proxyMethod 与源对象中的method相对应的代理对象中的方法 */ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("执行方法"+method+"前"); //执行源对象的method方法 Object returnValue = proxy.invokeSuper(obj, args); System.out.println("执行方法"+method+"后"); return returnValue; } }
代理工厂ProxyFactory.java
- import net.sf.cglib.proxy.Enhancer;
- public class ProxyFactory {
- /*
- * 获得代理对象
- */
- public static Object getProxyObj(String clazz) throws Exception{
- Class<?> superClass = Class.forName(clazz);
- Enhancer hancer = new Enhancer();
- //设置代理对象的父类
- hancer.setSuperclass(superClass);
- //设置回调对象,即调用代理对象里面的方法时,实际上执行的是回调对象(里的intercept方法)。
- hancer.setCallback(new MyMethodInterceptor());
- //创建代理对象
- return hancer.create();
- }
- }
import net.sf.cglib.proxy.Enhancer; public class ProxyFactory { /* * 获得代理对象 */ public static Object getProxyObj(String clazz) throws Exception{ Class<?> superClass = Class.forName(clazz); Enhancer hancer = new Enhancer(); //设置代理对象的父类 hancer.setSuperclass(superClass); //设置回调对象,即调用代理对象里面的方法时,实际上执行的是回调对象(里的intercept方法)。 hancer.setCallback(new MyMethodInterceptor()); //创建代理对象 return hancer.create(); } }
测试类:
- public class Test {
- public static void main(String[] args) {
- try {
- HelloWorld hello = (HelloWorld) ProxyFactory.getProxyObj(HelloWorld.class.getName());
- System.out.println(hello.sayHello("huangqiqing"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
public class Test { public static void main(String[] args) { try { HelloWorld hello = (HelloWorld) ProxyFactory.getProxyObj(HelloWorld.class.getName()); System.out.println(hello.sayHello("huangqiqing")); } catch (Exception e) { e.printStackTrace(); } } }
输出结果:
执行方法public java.lang.String HelloWorld.sayHello(java.lang.String)前
执行sayHello()
执行方法public java.lang.String HelloWorld.sayHello(java.lang.String)后
hello,huangqiqing
执行方法protected void java.lang.Object.finalize() throws java.lang.Throwable前
执行方法protected void java.lang.Object.finalize() throws java.lang.Throwable后