PlayDependencyClassLoader找不到应用程序代码 [英] PlayDependencyClassLoader does not find application code

查看:97
本文介绍了PlayDependencyClassLoader找不到应用程序代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当第三方依赖项尝试使用

When a third party dependency attempts to load a class defined in a Play application using

Class.forName(className, true, Thread.currentThread().getContextClassLoader());

播放将抛出ClassNotFoundException,因为上下文类加载器的类型为PlayDependencyClassLoader,显然它仅包含jar依赖项中定义的类.

Play will throw a ClassNotFoundException because the context class loader is of type PlayDependencyClassLoader which apparently only contains classes defined in jar dependencies.

Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: eventstore.Commit
    at org.mapdb.SerializerPojo.classForName(SerializerPojo.java:96)
    at org.mapdb.SerializerPojo$1.deserialize(SerializerPojo.java:74)
    at org.mapdb.SerializerPojo$1.deserialize(SerializerPojo.java:39)

仅当以play run开始播放时才会发生.从play start开始播放会正确加载该课程.

This only occurs when Play is started with play run. Starting Play with play start loads the class correctly.

由于这种行为而牺牲了类的热交换将是一种耻辱.有已知的解决方法吗?

It would be a shame to sacrifice the class hot-swapping because of this behavior. Is there a known workaround?

推荐答案

目前的hacky类型修复是使用以下功能包装使用Class.forName的第三方库的调用:

A hacky type fix for now is to wrap invocations of third party libraries that use Class.forName with a function like this:

  // see Play classloader bug https://github.com/playframework/playframework/issues/822
  def fixClassLoader[E](f: () => E) = {
    val old = Thread.currentThread().getContextClassLoader()
    try {
      Thread.currentThread().setContextClassLoader(Play.application.classloader)
      f()
    } finally {
      Thread.currentThread().setContextClassLoader(old)
    }
  }

这篇关于PlayDependencyClassLoader找不到应用程序代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆