The created API module with the name com.packt.service.api contains a NotificationService interface to send notification and load service providers. To make this interface a service provider interface (SPI), we have to mention the 'use' clause in module-info.java. Our module code will be as follows:
NotificationService.javapackage com.packt.service.api;import java.util.ArrayList;import java.util.List;import java.util.ServiceLoader;public interface NotificationService { /* Loads all the service providers */ public static List<NotificationService> getInstances() { ServiceLoader<NotificationService> services = ServiceLoader.load(NotificationService.class); List<NotificationService> list = new ArrayList<>(); services.iterator().forEachRemaining(list::add); ...