PlayDependencyClassLoader找不到应用程序代码 [英] PlayDependencyClassLoader does not find application code
问题描述
当第三方依赖项尝试使用
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屋!