[java-代理]测试Proxy和Enhancer两种代理方式

区别

Proxy是基于接口的方式进行代理,Enhancer是基于继承的方式代理。
proxy是java.lang.reflect.*
enhancer是net.sf.cglib.*

测试代码:

package main;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.xmlstore.ObjectData;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class EnhancerMainTest {

    public static void main(String[]args){
//        testEnchancer();
//        testProxy();
        testProxy2();
    }

    //测试 proxy
    public static void testProxy(){
        PrintInterface printInterface = (PrintInterface) new EnhancerProxy().bind(new EnhancerDemo());
        printInterface.print();
    }

    //测试 proxy2
    public static void testProxy2(){
        EnhancerDemo demo = new EnhancerDemo();
        PrintInterface enhancerDemo = (PrintInterface) Proxy.newProxyInstance(EnhancerDemo.class.getClassLoader(), EnhancerDemo.class.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("before: "+method);
                //此处invoke不可以直接使用proxy 否则会陷入递归
                Object result = method.invoke(demo, args);
                System.out.println("after: "+method);
                return result;
            }
        });
        enhancerDemo.print();
    }

    //测试 enchancer 代理
    public static void testEnchancer(){
        System.out.println("enhancer main test");
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(EnhancerDemo.class);
        enhancer.setUseCache(false);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("before "+method);
                //此处不可以使用invoke方法需使用invokeSuper否则会进入递归循环
                Object result = methodProxy.invokeSuper(o,objects);
                System.out.println("after "+method);
                return result;
            }
        });
        EnhancerDemo enhancerDemo = (EnhancerDemo) enhancer.create();
        enhancerDemo.print();
    }
}

//测试接口
interface PrintInterface{
    public void print();
}

//测试demo
class EnhancerDemo implements PrintInterface{

    public void print(){
        System.out.println("print");
    }
}

class EnhancerProxy implements InvocationHandler{
    private Object target;

    public Object bind(Object target){
        this.target = target;
        Object proxy = Proxy.newProxyInstance(this.target.getClass().getClassLoader(),this.target.getClass().getInterfaces(),this);
        return proxy;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        System.out.println("before");
        return method.invoke(this.target,objects);
    }
}
发布了170 篇原创文章 · 获赞 30 · 访问量 61万+

猜你喜欢

转载自blog.csdn.net/u010989191/article/details/55668888