How to do it...

We have four modules in this recipe:

  • book.service: This is the module that contains our service provider interface, that is, the service
  • mongodb.book.service: This is one of the service provider modules
  • sqldb.book.service: This is the other service provider module
  • book.manage: This is the service consumer module

The following steps demonstrate how to make use of ServiceLoader to achieve loose coupling:

  1. Create a book.service folder under the Chapter03/8_services/src directory. All our code for the book.service module will be under this folder. 
  2. Create a new package, com.packt.model, and a new class, Book, under the new package. This is our model class, which contains the following properties:
        public String id;  ...

Get Java 11 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.