zoukankan      html  css  js  c++  java
  • JAVA 两种代理实现

    import org.springframework.cglib.proxy.Enhancer;
    import org.springframework.cglib.proxy.MethodInterceptor;
    import org.springframework.cglib.proxy.MethodProxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    class JDKProxy implements InvocationHandler{
    
        private Object targetObject;
    
        public Object newProxy(Object targetObject){
            this.targetObject = targetObject;
            return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),targetObject.getClass().getInterfaces(),this);
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object ret = null;;
            ret = method.invoke(targetObject,args);
            return null;
        }
    }
    
    class CGLibProxy implements MethodInterceptor {
    
        private Object targetObject;
        public Object newProxy(Object object){
            this.targetObject = object;
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(object.getClass());
            enhancer.setCallback(this);
            return enhancer.create();
        }
    
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            Object ret = null;
            ret = method.invoke(targetObject,objects);
            return ret;
        }
    }
    

     

  • 相关阅读:
    RAD Studio最终版合集
    cxGrid 锁定一行,让该行数据不能编辑
    跨平台打开一个URL的方法
    【转】DELPHI开始支持LINUX DOCKER
    HTTP请求的拦截
    SVG图像
    Kafka
    HBase分布式集群部署
    HBase
    Mapreduce提交YARN集群运行
  • 原文地址:https://www.cnblogs.com/forbeat/p/13065667.html
Copyright © 2011-2022 走看看