美文网首页
测试Proxy和Enhancer两种代理方式(转)

测试Proxy和Enhancer两种代理方式(转)

作者: 西华子 | 来源:发表于2018-06-10 10:43 被阅读0次

    区别

    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);
        }
    }
    

    相关文章

      网友评论

          本文标题:测试Proxy和Enhancer两种代理方式(转)

          本文链接:https://www.haomeiwen.com/subject/jxjfeftx.html