Java - Service Provider Interface (SPI)

Java Conceptuel Diagram

About

SPI is an extension mechanism that implements a service provider framework.

See a whole simple example on GitHub

The whole SPI concept is construct round the loading of new provider with the ServiceLoader.

Concept

Service

A set of programming interfaces and classes that provide access to some specific application functionality or feature.

A service is a well-known set of interfaces and (usually abstract) classes.

Service Provider Interface

The set of public interfaces and abstract classes that a service defines. The SPI defines the classes and methods available to your application.

Service Provider

A service provider is a specific implementation of a service. The classes in a provider typically implement the interfaces and subclass the classes defined in the service itself.

Two same services

It's not an error to install more than one provider for the same service. For example, two different service providers might supply support for reading the same type of sound file. In such a case, the system arbitrarily chooses one of the providers. Users who care which provider is chosen should install only the desired one.

Service Management

Installation

Service providers supply their new services in specially formatted JAR files.

The service provider is declared through a provider-configuration file located in the resource directory META-INF/services

List

Example with FileSystemProvider.class that is the SPI interface of NIO.

ServiceLoader<FileSystemProvider> loader = ServiceLoader.load(FileSystemProvider.class);
for (FileSystemProvider provider: loader) {
    System.out.println("Provider: "+provider.class.toString()+" with scheme "+provider.getScheme());
}

ServiceLoader<FileSystemProvider> sl = ServiceLoader.load(FileSystemProvider.class, ClassLoader.getSystemClassLoader());
for (FileSystemProvider provider: sl) {
    System.out.println("Provider: "+provider.class.toString()+" with scheme "+provider.getScheme());
}

Documentation / Reference





Discover More
Card Puncher Data Processing
Application - Service Provider Framework

There are three essential components of a service provider framework: a service interface, which providers implement; a provider registration API, which the system uses to register implementations,...
Card Puncher Data Processing
Calcite (Farrago, Optiq)

Calcite is a Java SQL Processing engine where the data storage is developed in plugin. Calcite is an open source cost based query optimizer and query execution framework. SQL Parser SQL Validation...
Java Conceptuel Diagram
Java - Module

A Java module is a packaging format that create a modular JAR file. It's a feature of JDK 9 from the Java Platform Module System (JPMS) that divide the monolithic rt.jar and tools.jar files into 75 distinct...
Java Conceptuel Diagram
Vert.x - Metrics

in Vertx. By default Vert.x does not record any metrics. Instead it provides an SPI for others to implement which can be added to the classpath. The metrics SPI is an advanced feature which allows implementers...



Share this page:
Follow us:
Task Runner