How can we create a Service Provider interface in Java 9?

JavaObject Oriented ProgrammingProgramming

A module that provides the implementation for the Service interface contains a "provides" statement in the module descriptor file. If the module doesn’t have the "provides" statement in the module descriptor file, the service loader can't load that module.

We can create the Service Provider Interface by using below steps:

  • We create a new Module com.tutorialspoint.serviceproviderinterface.
  • In the src/main/java directory, we create "module-info.java" file.
  • Inside our source directory, we create the package com.tutorialspoint.serviceproviderinterface.spi.
  • Finally, we create the interface ServiceProviderInterface that contains a method: printServiceName() to be implemented.


In the below, we can define Service Provider Interface.

package com.tutorialspoint.serviceproviderinterface.spi;

public interface ServiceProviderInterface {
   void printServiceName();
}
raja
Published on 10-Apr-2020 15:49:26
Advertisements