如果查找返回null,该如何处理?我使用的是org.openide.util.Lookup的Lookup.getDefault().lookup(),它用于查找对象的实例。一般的模式是传递一个Class对象,然后返回该类的一个实例或null。
我在下面的代码中使用它:
StreamingServer server = Lookup.getDefault().lookup(StreamingServer.class);
ServerControllerFactory controllerFactory = Lookup.getDefault().lookup(ServerControllerFactory.class);
StreamingController controller = Lookup.getDefault().lookup(StreamingController.class);由于未找到实例,因此'server‘、'controllerFactory’、'controller‘返回null。我需要处理这种情况,以便可以使用这些对象来访问方法,如下所示:
controllerFactory.createServerController(graph)
server.register(serverController, context)我如何才能做到这一点呢?
发布于 2015-07-21 15:40:36
您需要在Eclipse源文件夹中创建一个META-INF/services文件夹。然后,为每个类在META-INF/services文件夹中创建一个文本文件。文本文件的名称是类类型的全名(包括包位置),文本文件的内容是实现类的全名(包括包位置)(不一定与类的名称相同)。
例如,对于ServerControllerFactory类,您将创建一个名为org.gephi.streaming.server.ServerControllerFactory的文本文件,该文件将包含的文本为org.gephi.streaming.server.impl.ServerControllerFactory
发布于 2015-10-27 02:50:04
对我来说,我试图在其上调用lookup的类只需要这样的@ServiceProvider装饰:
@ServiceProvider(service = MyClassHere.class)
public final class MyClassHere ....https://stackoverflow.com/questions/23643324
复制相似问题