简体   繁体   English

什么是ServiceLoader?如何使用?

[英]What is ServiceLoader and how is it used?

I came across the documentation of ServiceLoader and am unclear as to what use cases it suits. ServiceLoader的文档,不清楚它适合什么用例。

When would one use ServiceLoader ? 什么时候使用ServiceLoader

You use ServiceLoader when you want your program to have a “plugin” functionality. 希望程序具有“插件”功能时,可以使用ServiceLoader。 When you want to allow people to customize your application by adding jar files to the classpath which contain implementations of a specific subset of functionality, you can use a ServiceLoader to look for those implementations in the classpath. 如果要允许人们通过将jar文件添加到包含特定功能子集的实现的jar路径中来自定义应用程序,则可以使用ServiceLoader在classpath中查找那些实现。

ServiceLoader is itself an implementation of the jar SPI specification , which has been around for a long time. ServiceLoader本身是jar SPI规范的实现,该规范已经存在了很长时间。 (I believe it was introduced in Java 1.3.) (我相信它是在Java 1.3中引入的。)

Java SE already uses it for exactly that purpose in a lot of places, including: Java SE已经在很多地方将它用于此目的,包括:

Are you familiar with the principle "Inversion of Control" ? 您熟悉“控制反转”原理吗?

Java implemented it by ServiceLoader . Java通过ServiceLoader实现了它。 The class is designed to locate implementation classes of an interface on the classpath. 该类旨在在类路径上找到接口的实现类。 You pass in a service interface, you get an implementation(s) of that service. 传递服务接口,即可获得该服务的实现。

You might find a good practical example here . 您可能会在这里找到一个很好的实际例子。


PS: Even though it's an out-of-the-box solution and a quite simple tool, I think it's outdated and not flexible enough compared to the Spring IoC Container and Google Guice. PS:尽管它是一个即用的解决方案和一个非常简单的工具,但与Spring IoC Container和Google Guice相比,我认为它已经过时且不够灵活。

ServiceLoader is Java's light weight alternative to a full blown IoC container such as Spring, Guice, etc. It has far less bells and whistles than those frameworks, but works well for basic use cases when you just want to find what classes implement an interface. ServiceLoader是Java的轻量级替代品,可以替代Spring,Guice等完整的IoC容器。它比那些框架少很多,但是当您只想查找实现类的接口时,对于基本用例就很好用。

Most application servers will have some usages of ServiceLoader you can see in practice: 大多数应用程序服务器将具有ServiceLoader的一些用法,您可以在实践中看到这些用法:

https://github.com/apache/tomee/search?q=ServiceLoader&unscoped_q=ServiceLoader https://github.com/apache/tomee/search?q=ServiceLoader&unscoped_q=ServiceLoader

https://github.com/apache/tomcat/search?q=ServiceLoader&unscoped_q=ServiceLoader https://github.com/apache/tomcat/search?q=ServiceLoader&unscoped_q=ServiceLoader

https://github.com/wildfly/wildfly/search?q=ServiceLoader&unscoped_q=ServiceLoader https://github.com/wildfly/wildfly/search?q=ServiceLoader&unscoped_q=ServiceLoader

When I hear something about ServiceLoader , the first thing, which comes to my mind, is JDBC . 当我听到有关ServiceLoader ,想到的第一件事就是JDBC This technology provides loading JDBC driver classes from a classpath without using Class.forName(Class<?>clazz) . 此技术提供了从类路径加载JDBC驱动程序类的功能,而无需使用Class.forName(Class<?>clazz)

Also, I am sure there are many examples of using ServiceLoader , beside JDBC 另外,我确定除了JDBC之外 ,还有很多使用ServiceLoader示例。

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

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