博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 动态代理
阅读量:6932 次
发布时间:2019-06-27

本文共 877 字,大约阅读时间需要 2 分钟。

hot3.png

  • 编写Api接口文件
package com.uwo.resources.ui.proxy;/** * Created by yanhao on 2017/5/12. */public interface Api {    String user();}
  • 编写实现InvocationHandler接口
package com.uwo.resources.ui.proxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;/** * Created by yanhao on 2017/5/12. */public class UwoHandler implements InvocationHandler{    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        return "YH";    }}
  • 编写测试类
package com.uwo.resources.ui.proxy;import java.lang.reflect.Proxy;/** * Created by yanhao on 2017/5/12. */public class Test {    public static void main(String[] args){        Api user = (Api)Proxy.newProxyInstance(Test.class.getClassLoader(), new Class[]{Api.class}, new UwoHandler());        String s = user.user();        System.out.println(s);    }}

转载于:https://my.oschina.net/yan5845hao/blog/898429

你可能感兴趣的文章
Linux几个命令
查看>>
Linux系统日志级别
查看>>
Web报表工具FineReport中JavaScript的使用
查看>>
EMC virtual provisining and fast vp
查看>>
win10 uwp 如何打包Nuget给其他人
查看>>
活动目录之用户配置文件(转载)
查看>>
Git 远程分支的pull与push
查看>>
mysql单用户赋予多库权限
查看>>
Tcpdump配合Tcpreplay回放实现网络探测
查看>>
mysql多实例安装脚本
查看>>
iOS开发UI篇—UIWindow简单介绍
查看>>
apache+svn服务搭建
查看>>
在一个页面加载另一个渲染的页面
查看>>
linux 历史(histroy)命令记录过滤
查看>>
APACHE 安装出错 configure: error: Cannot use an external APR with the bundled APR-util
查看>>
PHP性能调优,PHP慢日志---PHP脚本执行效率性能检测之WebGrind的使用
查看>>
linux工具之curl与wget高级使用
查看>>
FFmpeg Installation for Ubuntu
查看>>
mysql中表名不区分大小写
查看>>
理解Docker——深入引擎室
查看>>