当我尝试使用动态代理时,我看到以下异常
 com.intellij.rt.execution.application.AppMain DynamicProxy.DynamicProxy
Exception in thread "main" java.lang.IllegalArgumentException: interface Interfaces.IPerson is not visible from class loader
    at java.lang.reflect.Proxy.getProxyClass(Proxy.java:353)
    at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
    at DynamicProxy.Creator.getProxy(Creator.java:18)
    at DynamicProxy.DynamicProxy.main(DynamicProxy.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
知道我需要做什么来解决它
omn*_*nom 11
If this is web application, then you should use the web application classloader when creating dynamic proxy. So, for example instead of:
Proxy.newProxyInstance(
  ClassLoader.getSystemClassLoader(),
  new Class < ? >[] {MyInterface.class},
  new InvocationHandler() {
    // (...)
});
try:
Proxy.newProxyInstance(
  this.getClass().getClassLoader(), // here is the trick
  new Class < ? >[] {MyInterface.class},
  new InvocationHandler() {
    // (...)
});
For instance, hierarchy of tomcat class loaders (other web containers have similar) is following:
      Bootstrap
          |
       System
          |
       Common
       /     \
  Webapp1   Webapp2 ... 
And it is the webapp classloader which contains classes and resources in the /WEB-INF/classes directory of your web application, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application.
当您DynamicProxy尝试执行Class.forName(youInterfaceClass.getName())生成的java.lang.Class实例时,与创建代理时传递的实例不同.换句话说,你有两个具有相同名称的类对象,代理不确定哪一个是正确的(无论它们是否相同).
通常,当您尝试代理的接口位于通过两个不同的类加载器加载的库中时(即Tomcat的'common'和'application'),会发生这种情况.
如果这没有帮助,请在您的应用程序上发布更多信息 - 特别是如果您使用的是任何应用程序服务器,Spring或OSGi.
| 归档时间: | 
 | 
| 查看次数: | 19790 次 | 
| 最近记录: |