Using the Java platform's ServiceLoader class to retrieve JPMS provider implementations
So far, we have configured the module
descriptor of the Application and Framework hexagon modules. We have refactored the input adapters so that they only depend on the abstractions provided by use cases interfaces. But how can we retrieve the concrete instances that implement those use cases interfaces? That's exactly what the ServiceLoader
class does.
ServiceLoader
is not a new class made solely to support JPMS features. Instead, ServiceLoader
has been present in Java since version 1.6. From Java 9 onward, this class was enhanced to work with the Java modules' services. It relies on the configuration provided by the module
descriptor to find implementations for a given service provider interface.
To illustrate how we can use ServiceLoader
, let's update the FrameworkTestData
test class by creating a method called loadPortsAndUseCases
. This method uses ServiceLoader
to...