Java's ServiceLoader
class is now officially baked into the Java language. Instead of looking for providers in META-INF/services
you can now use the
provides <spiClass> with <providerClass>
What I fail to understand is, the use of uses
in the service loading module declaration:
uses <spiClass>
Quoting from The State of the Module System
The module system could identify uses of services by scanning the class files in module artifacts for invocations of the
ServiceLoader::load
methods, but that would be both slow and unreliable. That a module uses a particular service is a fundamental aspect of that module’s definition, so for both efficiency and clarity we express that in the module’s declaration with a uses clause:module java.sql { requires transitive java.logging; requires transitive java.xml; exports java.sql; exports javax.sql; exports javax.transaction.xa; uses java.sql.Driver; }
Why is it fundamental for the module system to know uses of a particular service, especially how will this introduce efficiency? Aren't services loaded lazily? Why can't the service loader just look for providers on the fly?