先看一下Proxy的兩種用法:
public
?
interface
?Foo?
{
????
int
?doSomthing();
}
???一個(gè)基本的實(shí)現(xiàn):

public?class?FooImpl?implements?Foo?
{

????public?FooImpl()?
{
????}


????public?int?doSomthing()?
{
????????System.out.println("FooImpl?doSomthing
");
????????return?10;
????}
????
}???調(diào)用處理類(lèi),這也是aop的一種實(shí)現(xiàn)方式,呵呵。

public?class?MyInvocationHandler?implements?InvocationHandler
{
????Object?proxyObject;
????

????public?MyInvocationHandler(Object?_proxyObject)?
{
????????this.proxyObject=_proxyObject;
????}


????public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)?throws?Throwable?
{
????????System.out.println("in?proxy?instance
");
????????return?method.invoke(proxyObject,args);
????}
}???兩種實(shí)現(xiàn)方式:

public?class?ProxyUse?
{

????public?ProxyUse()?
{
????}

????public?void?useProxy1()?throws?SecurityException,?NoSuchMethodException,?InvocationTargetException,

????????????IllegalArgumentException,?IllegalAccessException,?InstantiationException?
{
????????FooImpl?obj?=?new?FooImpl();
????????InvocationHandler?handler?=?new?MyInvocationHandler(obj);
????????Class?proxyClass?=?Proxy.getProxyClass(

????????????????Foo.class.getClassLoader(),?new?Class[]?
{Foo.class});
????????Foo?f?=?(Foo)?proxyClass.

????????????????getConstructor(new?Class[]?
{InvocationHandler.class}).

????????????????newInstance(new?Object[]?
{handler});
????????System.out.println(f.doSomthing());
????}
????
????public?void?useProxy2()?throws?SecurityException,?NoSuchMethodException,?InvocationTargetException,

????????????IllegalArgumentException,?IllegalAccessException,?InstantiationException?
{
????????FooImpl?obj?=?new?FooImpl();
????????InvocationHandler?handler?=?new?MyInvocationHandler(obj);
????????Foo?f?=?(Foo)?Proxy.newProxyInstance(
????????????Foo.class.getClassLoader(),

????????????new?Class[]?
{?Foo.class?},
????????????handler
????????????);
????????System.out.println(f.doSomthing());
????}
????

????public?static?void?main(String?[]?args)
{
????????ProxyUse?use=new?ProxyUse();

????????try
{
????????????use.useProxy1();
????????????use.useProxy2();

????????}catch(Exception?ex)
{
????????????ex.printStackTrace();
????????}
????}

}看一下java api doc
沒(méi)時(shí)間了,先這樣吧。
?