初步看了java動態(tài)代理
ProxyClass.java
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;


public class ProxyClass
{
public static void main(String arags[]) throws IllegalArgumentException,
SecurityException, InstantiationException, IllegalAccessException,

InvocationTargetException, NoSuchMethodException
{
InvocationHandler handler = new MyInvocationHandler();
Class proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(),

new Class[]
{ Foo.class });
Foo f = (Foo) proxyClass.getConstructor(

new Class[]
{ InvocationHandler.class }).newInstance(

new Object[]
{ handler });
f.doSomething();

}
}
Foo.java

public interface Foo
{
void doSomething();
}MyInvocationHandler.java
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class MyInvocationHandler implements InvocationHandler
{

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable
{
System.out.println("Helloworld");
return null;
}

}





























Foo.java






















posted on 2006-01-04 16:32 bluesky 閱讀(1267) 評論(3) 編輯 收藏 所屬分類: 工作總結(jié)