Skip to content
Related Articles
Open in App
Not now

Related Articles

Java Modules – Service Interface Module

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 23 Nov, 2022
Improve Article
Save Article

Service Provider Interface, a feature of Java 6, makes it possible to find and load implementations that adhere to a specified interface. In this article, we’ll introduce Java SPI’s components and demonstrate how to use it in a real-world scenario. a well-known collection of programming classes and interfaces that give users access to a particular feature or functionality of an application. Applications are now more extendable thanks to the introduction of the Service Provider Interface. It provides us with a way to improve particular product features without changing the main application. All we have to do is plug in a new implementation of the service that adheres to the established requirements. The program will load the new performance and use it by means of the SPI protocol.

Let’s Understand a few Terms First

  • Service Provider Interface: Service Provider Interface is referred to as SPI. It is a subset of everything that may be API-specific in circumstances where a library offers classes that an application (or API library) calls and that typically alter what the application is able to do.
  • Service Provider: A particular service implementation is referred to as a “provider” as well. By putting the provider configuration file in the resources directory META-INF/services, it can be located. It must be accessible through the classpath of the application.
  • ServiceLoader: A class that implements the well-known interface or subclasses it is referred to as a service provider (or simply a provider). When an application chooses, a ServiceLoader is an object that finds and loads service providers deployed in the run time environment.

A particular application of the SPI. One or more concrete classes that implement or extend the service type are present in the service provider. A provider configuration file that we place in the resource directory META-INF/services allows us to configure and identify a service provider. The fully-qualified name of the SPI is contained in both the file name and its content, which is the name of the SPI implementation. The Service Provider is installed using extensions, a jar file that is added to the application classpath, the classpath for Java extensions, or a custom classpath. Now Let’s see the Example.


In this example, we will implement a service interface module in java using the classic classics library module. this program implementation will have access to the getBook() method.


<!-- We're including all the 
     dependencies here in this program -->

Then we will create a class that will implement the SPI library.


package org.library;
// Inheriting the class
public class ClassicsLibrary implements Library {
    public static final String Classic_Library
        = "Classic_Example";
    private final Map<String, Book> books;
    // ClassicsLibrary() method declaration
    public ClassicsLibrary()
        books = new TreeMap<>();
        Book Example_1
            = new Book("It's 2022", "Mr. Sinha", "Des");
        Book Example_2 = new Book("It's EG2 book Name",
                                  "Mis Sinha", "Des");
        books.put("It's 2022", Example_1);
        books.put("It's EG2 book Name", Example_2);
    @Override public String getCategory()
        return Classic_Library;
    @Override public Book getBook(String name)
        return books.get(name);

It should be evident how to use the Java SPI to develop readily expandable or replacement modules now that we have investigated the mechanism through a set of stated steps. Although the Yahoo exchange rate service was used in our example to demonstrate the capability of connecting to other external APIs, production systems don’t need to rely on third-party APIs to develop fantastic SPI applications.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!