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");

解决方案 »

  1.   

    JVM provides more class loaders. The loader loading the Object Class is different with the loader loading the proxied object class.
      

  2.   

    这倒不一定,上面正常情况,代理类和接口的ClassLoader是同一个
    上面不正常的情况的错误是Exception in thread "main" java.lang.IllegalArgumentException: interface com.proxy.SayHello is not visible from class loader
      

  3.   

    Please look at the newInstance() method in the Proxy Class. public static Object newProxyInstance(ClassLoader 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.
      

  4.   

    Sorry, I'm in the office. I can't reply in Chinese
      

  5.   

    you say if i right.i pass a classloader,then jvm will extends the classloader i passed, use this classloader to define the proxy class, of course the classLoader Object.class.getClassLoader() returns cannot load then class SayHello
      

  6.   

    Try to print the "Object.class.getClassLoader()". It's null