Service (API) module

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); ...

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.