SayHello say = new SayHelloImpl();
LoggerHandler h = new LoggerHandler(say);
SayHello s = (SayHello)Proxy.newProxyInstance(SayHello.class.getClassLoader(), SayHelloImpl.class.getInterfaces(), h);
s.print("zhangsan");参数ClassLoader只起定义类的作用,那为什么不能是这样?
SayHello say = new SayHelloImpl();
LoggerHandler h = new LoggerHandler(say);
SayHello s = (SayHello)Proxy.newProxyInstance(Object.class.getClassLoader(), SayHelloImpl.class.getInterfaces(), h);
s.print("zhangsan");
上面不正常的情况的错误是Exception in thread "main" java.lang.IllegalArgumentException: interface com.proxy.SayHello is not visible from class loader
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
if (h == null) {
throw new NullPointerException();
} /*
* Look up or generate the designated proxy class.
*/
Class cl = getProxyClass(loader, interfaces);
.......Here it gets the proxy class object('SayHello' class object) by the class loader and interfaces. If the interface doesn't exist in the class loader you passed. The IllegalArgumentException will be thrown.