package com.haizhitao.dynamicproxy;
import java.lang.reflect.Proxy;
public class Demo
{
public static void main(String[] args)
{
CommonInvocationHandler handler = new CommonInvocationHandler();
Foo f = null;
handler.setTarget(new FooImpl());
f = (Foo)Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[]{Foo.class}, handler);
f.doAction();
System.out.println("-----------------------");
handler.setTarget(new FooImpl2());
f = (Foo)Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[]{Foo.class}, handler);
f.doAction();
}
}
//output:
// in FooImpl doAction!
// -----------------------
// in FooImpl2 doAction!
22
最新推荐文章于 2024-07-22 00:43:49 发布