本文共 6890 字,大约阅读时间需要 22 分钟。
java spi
Java SPI (Service Provider Interface) is the mechanism to load services dynamically. We can implement Java SPI in our application by following the specific set of rules and load the services using the ServiceLoader class.
Java SPI(服务提供者接口)是动态加载服务的机制。 通过遵循特定的规则集,我们可以在我们的应用程序中实现Java SPI,并使用ServiceLoader类加载服务。
There are four components in the SPI implementation.
SPI实现中包含四个组件。
The java.util.spi
package provides a lot of service provider interfaces that can be implemented to provide services.
java.util.spi
包提供了许多服务提供程序接口,可以将其实现以提供服务。
Let’s create an implementation of SPI and load some services using the ServiceLoader class.
让我们创建SPI的实现,并使用ServiceLoader类加载一些服务。
Let’s say we have a MessageServiceProvider
interface that defines the contract for the service provider implementations.
假设我们有一个MessageServiceProvider
接口,它定义了服务提供者实现的合同。
package com.journaldev.serviceproviders;public interface MessageServiceProvider { void sendMessage(String message);}
We want to support email messages and push notification messages. So we will create two service provider implementations of MessageServiceProvider
interface – EmailServiceProvider and PushNotificationServiceProvider.
我们希望支持电子邮件和推送通知消息。 因此,我们将创建MessageServiceProvider
接口的两个服务提供程序实现-EmailServiceProvider和PushNotificationServiceProvider。
package com.journaldev.serviceproviders;public class EmailServiceProvider implements MessageServiceProvider { public void sendMessage(String message) { System.out.println("Sending Email with Message = "+message); }}
package com.journaldev.serviceproviders;public class PushNotificationServiceProvider implements MessageServiceProvider { public void sendMessage(String message) { System.out.println("Sending Push Notification with Message = "+message); }}
The configuration file has to be created in the META-INF/services directory. Its name should be “com.journaldev.serviceproviders.MessageServiceProvider“. We will specify both the implementation classes in this file.
必须在META-INF / services目录中创建配置文件。 其名称应为“ com.journaldev.serviceproviders.MessageServiceProvider ”。 我们将在此文件中指定两个实现类。
com.journaldev.serviceproviders.EmailServiceProvidercom.journaldev.serviceproviders.PushNotificationServiceProvider
Finally, we have to load the services using the ServiceLoader class. Here is a simple test program showing its usage.
最后,我们必须使用ServiceLoader类加载服务。 这是一个显示其用法的简单测试程序。
package com.journaldev.test;import java.util.Optional;import java.util.ServiceLoader;import com.journaldev.serviceproviders.MessageServiceProvider;public class ServiceLoaderTest { public static void main(String[] args) { ServiceLoaderserviceLoader = ServiceLoader.load(MessageServiceProvider.class); for (MessageServiceProvider service : serviceLoader) { service.sendMessage("Hello"); } // using Java 8 Optional to get the first service Optional firstService = serviceLoader.findFirst(); if (firstService.isPresent()) { firstService.get().sendMessage("Hello Friend"); } // using Java 8 forEach() method serviceLoader.forEach((service) -> service.sendMessage("Have a Nice Day!")); // Total Number of Loaded Services System.out.println(serviceLoader.stream().count()); }}
When we run the above program, we get the following output.
当我们运行上面的程序时,我们得到以下输出。
Sending Email with Message = HelloSending Push Notification with Message = HelloSending Email with Message = Hello FriendSending Email with Message = Have a Nice Day!Sending Push Notification with Message = Have a Nice Day!2
The below image shows our final project structure and SPI components.
下图显示了我们的最终项目结构和SPI组件。
Let’s look at the important methods of the ServiceLoader class.
让我们看一下ServiceLoader类的重要方法。
Java SPI provides an easy way to dynamically configure and load the services in our application. However, it depends a lot on the service configuration file and any change in the file can break the application.
Java SPI提供了一种简单的方法来动态配置和加载应用程序中的服务。 但是,这在很大程度上取决于服务配置文件,并且文件中的任何更改都可能破坏应用程序。
翻译自:
java spi
转载地址:http://hyqzd.baihongyu.com/