美文网首页
测试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两种代理方式(转)

    区别 Proxy是基于接口的方式进行代理,Enhancer是基于继承的方式代理。proxy是java.lang.r...

  • CGLib

    1. Enhancer Enhancer是JDK动态代理的替代方法,既可以代理接口,又可以代理类。Enhancer...

  • 轻量级框架之Spring动态代理(第四周)

    动态代理主要有两种方式: JDK动态代理(Proxy) CGLIB代理 一、JDK动态代理 使用动态代理的五大步骤...

  • 反向代理(Reverse Proxy)

    简介: 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转...

  • ES--6Proxy和Reflect

    proxy代理 将原始数据代理,不能修改原始数据,通过代理的方式去修改数据。 Reflect 应用 Proxy可以...

  • ProxyFactory 之 CGLib 代理分析

    1. Enhancer 的基本使用 原生直接使用 Enhancer 的话,测试代码如下 基础使用的话基本和JDK ...

  • 使用apache 2.2 mod_proxy做tomcat we

    apache的配置 mod_proxy 支持转发代理和反向代理,所以配置反向代理时首先需要关闭转发代理,关闭方式见...

  • 1、Proxy代理模式

    1、Proxy 代理模式 代理(Proxy)提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好...

  • charles知识点总结

    配置与测试,常用功能如下: 配置代理: proxy--->proxy settings 设置端口 访问控制: pr...

  • 基于SOCK5之上的HTTP代理

    在linux和mac下,好多程序都可以通过设置HTTP_PROXY和HTTPS_PROXY的方式来进行HTTP代理...

网友评论

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

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