document 一:public interface Subject {
public void doAction();
}document 二:import com.staticproxy.Subject;public class RealSubject implements Subject { @Override
public void doAction() {
System.out.println("From RealSubject!");
}}document 三:import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;public class MyProxy implements InvocationHandler {
private Object object; public MyProxy(Object object) {
super();
this.object = object;
} //通过动态代理,将调用方法转给 InvocationHandler 类型的 handler.
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
this.before();
method.invoke(object, args);
this.after();
return null;
} private void before() { //额外增加的
System.out.println("Before doing");
} private void after() { //额外增加的.
System.out.println("After doing");
}}
 document 四:import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;public class Client {
public static void main(String[] args) throws Exception {
RealSubject realSubject = new RealSubject(); // 真实角色. InvocationHandler handler = new MyProxy(realSubject);
// 为了生成 DynamicProxy. Class<?> classType = handler.getClass(); // 下面的代码一次性生成代理 Subject subject = (Subject) Proxy.newProxyInstance(classType
.getClassLoader(), realSubject.getClass().getInterfaces(),
handler); subject.doAction(); }}
为什么总是 :
Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.dynamicproxy.Subject
找不到哪里错,应该是对的,崩溃。

解决方案 »

  1.   

    MyProxy的invoke方法里错了!
    不是return null.
    应该是Object obj = method.invoke(object, args);reutrn obj;
      

  2.   


    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;public class MyProxy implements InvocationHandler {
    private Object object;public MyProxy(Object object) {
    super();
    this.object = object;
    }//通过动态代理,将调用方法转给 InvocationHandler 类型的 handler.
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    this.before();
    method.invoke(object, args);
    this.after();
    return null;
    }private void before() { //额外增加的
    System.out.println("Before doing");
    }private void after() { //额外增加的.
    System.out.println("After doing");
    }}
    这一部分首先有问题,大概应该这样:
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;public class MyProxy implements InvocationHandler {//通过动态代理,将调用方法转给 InvocationHandler 类型的 handler.
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    this.before();
    Object result = method.invoke(proxy, args);
    this.after();
    return result;
    }private void before() { //额外增加的
    System.out.println("Before doing");
    }private void after() { //额外增加的.
    System.out.println("After doing");
    }}
    然后下面这一句:Subject subject = (Subject) Proxy.newProxyInstance(classType
    .getClassLoader(), realSubject.getClass().getInterfaces(),
    handler);
    改成这样试试:Subject subject = (Subject) Proxy.newProxyInstance(
      realSubject.getClass().getClassLoader(), 
      new Class<?>[]{ Subject.class },
      new MyProxy()
    );
      

  3.   

    不行的,编译都不过,
    后面的参数应该是 InvocationHandler 类型的,你这样强制类型转换语法也错了,
    而且按你该的,异常更多。
    ·····那位大哥看看下····
      

  4.   


    什么地方编译通不过?把你那句 InvocationHandler handler = new MyProxy(realSubject); 去掉,改了 MyProxy 类以后这一句当然通不过。
      

  5.   


    恩,试了一下,我刚刚说的的确不对……
    下面这个可以:
    public interface Subject {  public void doAction();
    }public class RealSubject implements Subject {  @Override
      public void doAction() {
        
        System.out.println("From RealSubject!");
      }
    }public class MyProxy implements InvocationHandler {
      
      private Object o;
      MyProxy(Object o) {
        
        this.o = o;
      }
      
      @Override
      public Object invoke(Object proxy, Method method, Object[] args)
              throws Throwable {
        
        before();
        Object result = method.invoke(o, args);
        after();
        return result;
      }  private void before() {
        
        System.out.println("Before doing");
      }  private void after() {
        
        System.out.println("After doing");
      }
    }public class Client {  public static void main(String[] args) throws Exception {
        
        RealSubject realSubject = new RealSubject();     Subject subject = (Subject)Proxy.newProxyInstance(
                
                realSubject.getClass().getClassLoader(), 
                new Class<?>[]{ Subject.class },
                new MyProxy(realSubject)
        );
        subject.doAction();
      }
    }
      

  6.   

    我也好奇怪,我检查了好久,甚至都用 CVS 去检查我的代码。
    (与教程里面的代码用 CVS做比较。就他的能运行,我的不能。崩溃!
    编译当然没有问题。毕竟是 ClassCastException 。
      

  7.   

    楼主的代码没有问题。能出现这种错误除非你的RealSubject不是实现com.dynamicproxy包下的Subject接口
      

  8.   

    高人,确实是,我有两个包,一个是:staticproxy ,另一个是 dynamicproxy··