将ServiceLoader配置文件放在Web应用程序中的位置

将ServiceLoader配置文件放在Web应用程序中的位置

问题描述:

我正在用Eclipse编写一个Web应用程序。

I am writing a web app in Eclipse.

我正在尝试使用ServiceLoader类加载某些插件。

I am trying to use the ServiceLoader class to load some plugins.

ServiceLoader的文档说我需要在META-INF / services中放置一个文件。

The docs for ServiceLoader say I need to place a file in META-INF/services.

我已经将文件放置在WebContent / META-INF中/ service文件夹,但是当我通过Eclipse运行JUnit测试时,找不到任何插件。

I have placed the file in the WebContent/META-INF/service folder but when I run the JUnit test via Eclipse it does not find any plugins.

这是文件的正确位置吗?

Is this the correct location for the file?

此外,如何从ServiceLoader中获取更多调试信息,例如正在搜索文件的文件夹?

Also, how can I get more debug info from ServiceLoader such as the folders it is searching for the file in?

META-INF / services应该位于用于查找插件的类加载器的类路径中的jar文件或资源目录的根目录。

The META-INF/services should be at the root of the jar file or resource directory in the classpath of the classloader used to locate plugins.

如果您有一个Web应用程序,那么您可以具有以下目录结构:

If you have a web application, then you can have directory structure like:

<webappname>/WEB-INF/classes/META-INF/services

但是理想的方法是将其放在插件的jar文件。例如,如果您有一个捆绑为 WEB-INF / lib / myplugin.jar 的插件,并且您的插件类为 com.example.plugin.MyPlugin 然后,该jar应该具有以下结构:

But the ideal way is to have it in your plugin's jar file. E.g if you have a plugin bundled as WEB-INF/lib/myplugin.jar, and your plugin class is com.example.plugin.MyPlugin Then the jar should have a structure:

myplugin.jar!/META-INF/services/com.example.plugin.MyPlugin

完成此操作后,您可以使用网络应用程序的类获取服务加载程序加载程序

Having done that, you can get a service loader using your web application's class loader