简体   繁体   English

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

[英]Where to put ServiceLoader config file in a web app

I am writing a web app in Eclipse. 我正在Eclipse中编写一个Web应用程序。

I am trying to use the ServiceLoader class to load some plugins. 我正在尝试使用ServiceLoader类来加载一些插件。

The docs for ServiceLoader say I need to place a file in META-INF/services. ServiceLoader的文档说我需要在META-INF / services中放置一个文件。

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. 我已将文件放在WebContent / META-INF / service文件夹中,但是当我通过Eclipse运行JUnit测试时,它找不到任何插件。

Is this the correct location for the file? 这是文件的正确位置吗?

Also, how can I get more debug info from ServiceLoader such as the folders it is searching for the file in? 另外,如何从ServiceLoader获取更多调试信息,例如它正在搜索文件的文件夹?

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. META-INF / services应位于用于定位插件的类加载器的类路径中的jar文件或资源目录的根目录。

If you have a web application, then you can have directory structure like: 如果您有一个Web应用程序,那么您可以拥有以下目录结构:

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

But the ideal way is to have it in your plugin's jar file. 但理想的方法是将它放在插件的jar文件中。 Eg 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: 例如,如果你有一个捆绑为WEB-INF/lib/myplugin.jar的插件,你的插件类是com.example.plugin.MyPlugin那么jar应该有一个结构:

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 完成后,您可以使用Web应用程序的类加载器获取服务加载器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM