欢迎来到小米的博客
希望能和您交流Java编程的知识和见解
BlogJava
首页
新随笔
联系
聚合
管理
随笔-57 评论-170 文章-17 trackbacks-0
JDK Dynamic Proxy模式的简单范例
在JDK1.3版本中引入了Dynamic Proxy的代理机制,通过实现java.lang.reflect.InvocationHandler接口,可以实现拦截需要改写的方法。下面是一个简单范例。
有下面一个接口TestInterface和它的一个实现TestImpl:
package sample.proxy;
/**/
/*
*
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public
interface
TestInterface
{
public
String print();
}
package sample.proxy;
/**/
/*
*
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public
class
TestImpl implements TestInterface
{
public
String print()
{
return
"
Hello, it's from TestImpl class
"
;
}
}
下面拦截print方法,调用自己的实现,这需要实现java.lang.reflect.InvocationHandler接口。
package sample.proxy;
import java.lang.reflect.
*
;
/**/
/*
*
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public
class
TestHandler implements InvocationHandler
{
TestInterface test;
/**/
/*
*
* 将动态代理绑定到指定的TestInterface
* @param test TestInterface
* @return TestInterface 绑定代理后的TestInterface
*/
public
TestInterface bind(TestInterface test)
{
this
.test
=
test;
TestInterface proxyTest
=
(TestInterface) Proxy.newProxyInstance(
test.getClass().getClassLoader(), test.getClass().getInterfaces(),
this
);
return
proxyTest;
}
/**/
/*
*
* 方法调用拦截器,拦截print方法
* @param proxy Object
* @param method Method
* @param args Object[]
* @return Object
* @throws Throwable
*/
public
Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
//
如果调用的是print方法,则替换掉
if
(
"
print
"
.equals(method.getName()))
{
return
"
HaHa, It's come from TestHandler
"
;
}
else
{
return
method.invoke(
this
.test, args);
}
}
}
下面是测试用例:
package sample.test;
import junit.framework.
*
;
import sample.proxy.
*
;
/**/
/*
*
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public
class
TestDynamicProxy extends TestCase
{
private
TestInterface test
=
null
;
protected
void
setUp() throws Exception
{
super.setUp();
TestHandler handler
=
new
TestHandler();
//
用handler去生成实例
test
=
handler.bind(
new
TestImpl());
}
protected
void
tearDown() throws Exception
{
test
=
null
;
super.tearDown();
}
public
void
testPrint()
{
System.
out
.println(test.print());
}
}
运行测试用例,可以看到输出的是“HaHa, It's come from TestHandler”。
posted on 2005-05-24 17:47
小米
阅读(1261)
评论(2)
编辑
收藏
所属分类:
Java
评论:
#
re: JDK Dynamic Proxy模式的简单范例 2005-10-20 13:10 |
鸟不生蛋蛋的地方
if ("print".equals(method.getName())) {
return "HaHa, It's come from TestHandler";
} else {
return method.invoke(this.test, args);
为什么这里的返回改成return method.invoke(proxy, args);就不行,这两个应该应用同一个对象吧,请教
回复
更多评论
#
re: JDK Dynamic Proxy模式的简单范例
2005-12-08 11:11 |
寂寞邀请
操作顺序 client->proxy->this.test->实际的操作
换成return method.invoke(proxy, args),就死循环了。
client->proxy->proxy->proxy...........
回复
更多评论
IT新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
相关文章:
由JComponent生成BufferedImage
如何将BufferedImage实例保存为BMP文件
关闭JBuilder2005的Smart MemberInsight功能
用JFreeChart画柱状图的范例
用ChartDirector在JSP中画统计图
用java.util.Timer定时执行任务
JavaMail的简单实例
如何比较两个有可能为null的实例
从SocketChannel对象池中获取的实例,使用时应注意的问题
JDK Dynamic Proxy模式的简单范例
小米,生活在深圳,专注于Java,主要从事数据库和网页编程。现在在学习着Hibernate和Spring。喜欢游戏、音乐和台球。联系方式:georgehill@21cn.com
<
2005年5月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(21)
给我留言
查看公开留言
查看私人留言
随笔分类
Hibernate(15)
Java(17)
Spring(1)
Struts(5)
其它(5)
数据库(2)
生活随笔(12)
随笔档案
2006年4月 (1)
2006年3月 (1)
2005年8月 (1)
2005年7月 (11)
2005年6月 (13)
2005年5月 (30)
文章分类
Eclipse(1)
Java(8)
其它(8)
文章档案
2005年7月 (1)
2005年6月 (13)
2005年5月 (3)
我的朋友们
emu的博客
Java BY
我的链接
Java Research
SUN Java技术中文社区
拯救程序员王俊
搜索
积分与排名
积分 - 96753
排名 - 83
最新评论
1. re: 如何在Struts中实现分页显示数据(2)
评论内容较长,点击标题查看
--小文
2. re: 我的网页附加码实现
hgjhg
--hjhgj
3. re: 《深入浅出Hibernate》读书笔记(2)——实体对象识别[未登录]
详细点就更好了
--moonandsun
4. re: 《深入浅出Hibernate》读书笔记(1)——实体对象生命周期[未登录]
哈哈,不错哦
--moonandsun
5. re: BMP文件格式
thank u very much
--spiet
阅读排行榜
1. 用java.util.Timer定时执行任务(8500)
2. 《深入浅出Hibernate》读书笔记(3)——数据缓存(4738)
3. 《深入浅出Hibernate》读书笔记(2)——实体对象识别(4494)
4. 《深入浅出Hibernate》读书笔记(5)——持久层操作(4162)
5. 《深入浅出Hibernate》读书笔记(4)——事务管理(4112)
评论排行榜
1. 如何在Struts中实现分页显示数据(2)(24)
2. 献出一份爱心 共同援助重病程序员王俊(22)
3. Struts的国际化完整解决方案(11)
4. 2005年6月27日,一个值得纪念的日子(9)
5. 《深入浅出Hibernate》读书笔记(1)——实体对象生命周期(8)