java动态代理dynamicproxy

上传人:bao****ty 文档编号:116638815 上传时间:2019-11-16 格式:DOC 页数:4 大小:28KB
返回 下载 相关 举报
java动态代理dynamicproxy_第1页
第1页 / 共4页
java动态代理dynamicproxy_第2页
第2页 / 共4页
java动态代理dynamicproxy_第3页
第3页 / 共4页
java动态代理dynamicproxy_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《java动态代理dynamicproxy》由会员分享,可在线阅读,更多相关《java动态代理dynamicproxy(4页珍藏版)》请在金锄头文库上搜索。

1、java动态代理DynamicProxy1.被代理对象的接口:1 package test.dynamicproxy;23 public interface TargetInterface 4 public void SayHello();5 public int sum(int a ,int b);6 2.被代理的对象:01 package test.dynamicproxy;0203 public class Target implements TargetInterface 0405 public void SayHello()06 System.out.println(“Hello”)

2、;07 08 public int sum(int a, int b) 09 return a+b;10 11 3.InvocationHandler包装:01 package test.dynamicproxy;0203 import java.lang.reflect.InvocationHandler;04 import java.lang.reflect.Method;0506 public class TargetInvocationHandler implements InvocationHandler 0708 private Object object;09 public Ta

3、rgetInvocationHandler(Object obj)10 this.object=obj;11 1213 public Object invoke(Object proxy, Method method, Object args2)14 throws Throwable15 16 doBefore();17 Object result = method.invoke(object, args2);18 doAfter();19 return result;20 2122 public void doBefore()23 System.out.println(“do before”

4、);24 2526 public void doAfter()27 System.out.println(“do after”);28 29 4.测试类:01 package test.dynamicproxy;0203 import java.lang.reflect.Proxy;0405 public class TestDynamicProxy 0607 /*08 * param args09 */10 public static void main(String args) 11 Target t=new Target();12 TargetInvocationHandler handler=new TargetInvocationHandler(t);1314 TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(15 t.getClass().getClassLoader(),16 t.getClass().getInterfaces(),17 handler);1819 proxy.SayHello();2021 int b=proxy.sum(10, 20);22 System.out.println(b);23 2425

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号