使用动态代理在接口中获取方法名称(不实现)

时间:2014-02-05 11:59:05

标签: java scala dynamic-proxy

我有一个像这样的简单界面

public interface Arithmatic {

    public long add(int num1, int num2) ;

    public long multiply(int num1, int num2) ;

  }

我需要使用java动态代理调用接口的方法名而不实现它。如何为此实施Invocation Handlernew Proxy instance

我必须在我的项目中使用scala执行该任务。

我的代理实例就像这样

val impl = Proxy.newProxyInstance(

      classOf[ Arithmatic].getClassLoader,
      classOf[ Arithmatic].getClasses,
      handler

    ).asInstanceOf[ Arithmatic]

就像在java中一样,我无法将其转换为Arithmatic。它给了我

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to Arithmatic

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)