开发者社区> 问答> 正文

动态代理是怎样实现的呢?怎么操作?

已解决

动态代理是怎样实现的呢?怎么操作?

展开
收起
游客a3frllpzccb7m 2022-04-02 09:01:06 1135 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    public class ProxyInovationHandler implements

    InvocationHandler{

    private Object target;//目标对象--真实对象

    public void setTarget(object target){

    this.target = target;

    }

    /**

    public object getproxyo(){

    return

    Proxy.newProxyinstance(this.hetClass(),getclassloader(),

    target,getclass() getintertaces(),this);

    /**

    *Proxy是代理类

    *method 代理类的调用处理程序的方法对象

    **/

    @overide

    public object invoke(object proxy, method method,object [ ]

    args)

    throws Throwable(

    log(method.getName());

    Object result= method.invoke(target, args);

    return result;

    }

    public void log(string methodtame){

    throws Throwable

    { log(method.getName());

    Object result=methodinvoke(target,args) ;

    return result;

    }

    public void log(string methodName){

    System.out.println(""执行""+methodName+""方法

    "");

    }

    }

    Chent.java 客户

    public class client {

    public static void main(string[ ] args){

    Host host =new Host();

    Proxy proxy=new Proxy(host);

    proxy.rent();

    }

    }

    2022-04-02 09:46:22
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JAVA反射原理以及一些常见的应用 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载