会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Find Sun
Personal
Java 动态代理示例
public
interface
SomeClass
{
public
abstract
void
someMethod();
public
abstract
void
someOtherMethod(final String text);
}
public
class
SomeClassImpl implements SomeClass
{
private
String userName;
public
SomeClassImpl(final String userName)
{
this
.userName
=
userName;
}
public
void
someMethod( )
{
System.
out
.println(
this
.userName);
}
public
void
someOtherMethod(final String text)
{
System.
out
.println(text);
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public
class
MethodCountingHandler implements InvocationHandler
{
/**/
/*
whatever object, you can pass one in
*/
private
final Object impl;
private
int
invocationCount
=
0
;
/**/
/*
constructor
*/
public
MethodCountingHandler(final Object impl)
{
this
.impl
=
impl;
}
/**/
/*
export the invocation Count
*/
public
int
getInvocationCount( )
{
return
invocationCount;
}
/**/
/*
implements the interface function of InvocationHandler
*/
public
Object invoke(Object proxy, Method meth, Object[] args) throws Throwable
{
try
{
this
.invocationCount
++
;
Object result
=
meth.invoke(impl, args);
return
result;
}
catch
(final InvocationTargetException ex)
{
throw
ex.getTargetException( );
}
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public
class
SomeClassFactory
{
public
static
final SomeClass getDynamicSomeClassProxy( )
{
/**/
/*
get a implement instance of SomeClass
*/
SomeClassImpl impl
=
new
SomeClassImpl(System.getProperty(
"
user.name
"
));
if
(
!
(impl instanceof SomeClass) )
return
null
;
/**/
/*
construct a invocation handler with the impl instance
*/
InvocationHandler handler
=
new
MethodCountingHandler(impl);
/**/
/*
get the class info, and the class loader used by this factory
*/
Class[] interfaces
=
new
Class[]
{ SomeClass.
class
}
;
ClassLoader loader
=
SomeClassFactory.
class
.getClassLoader( );
/**/
/*
* install the handler for all implementations of this interface in this class loader
* and return the proxy instance which accords to SomeClass interface.
*/
SomeClass proxy
=
(SomeClass)Proxy.newProxyInstance(loader,
interfaces,
handler);
return
proxy;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public
class
DemoDynamicProxy
{
public
static
final
void
main(final String[] args)
{
SomeClass proxy
=
SomeClassFactory.getDynamicSomeClassProxy( );
proxy.someMethod( );
proxy.someOtherMethod(
"
Our Proxy works!
"
);
/**/
/*
get the handler associated with this proxy instance
*/
InvocationHandler handler
=
Proxy.getInvocationHandler(proxy);
if
(handler instanceof MethodCountingHandler)
{
System.
out
.println(((MethodCountingHandler)handler).getInvocationCount( ));
}
}
}
Java的动态代理只支持基于Interface的Method Interception. 本例是从”Hardcore Java”一书中摘取出来的.
posted on
2004-07-28 05:20
findsun
阅读(
978
) 评论(
1
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
导航
博客园
首页
新随笔
联系
订阅
管理
公告