can't cast to implemented interface

前端 未结 2 1117
忘掉有多难
忘掉有多难 2020-11-29 08:55

i\'m very confused...

I have a class which directly implements an interface:

public class Device implements AutocompleteResult
{...}
<
2条回答
  •  悲哀的现实
    2020-11-29 09:37

    AKA when Java apparently doesn't Java.

    I hit this problem recently with Play Framework 2.6.3, what helped me was this: https://www.playframework.com/documentation/2.6.x/ThreadPools#Application-class-loader

    I leave this info here for the people that might have the same problem.

    To make it clearer, what helps is:

    Injecting Application on an Eager Singleton and then using its classloader to load the classes I was having issues with.

    To make it clearer

    public class Module {
    
    
     @Override
     public void configure {
       bind(TheClassLoaderLoader.class).asEagerSingleton()
    
    public static class TheClassLoaderLoader {
      @Inject
            public TheClassLoaderLoader( Application application) {
    
             ClassLoader classloader = application.classloader();
    
                    Class interfaceClass = classloader.loadClass(InterfaceClass.class.getName());
                    classloader.loadClass(ImplementsInterfaceClass.class.getName()).asSubclass(interfaceClass);
    

    The example here https://playframework.com/documentation/2.6.x/JavaDependencyInjection#Configurable-bindings

    That uses Environment often throws a frustrating ClassNotFoundException

    Cheers

提交回复
热议问题