module - What's the purpose of 'uses' directive in Java 9? -


java's serviceloader class officially baked java language. instead of looking providers in meta-inf/services can use

provides <spiclass> <providerclass> 

what fail understand is, use of uses in service loading module declaration:

uses <spiclass> 

quoting the state of module system

the module system identify uses of services scanning class files in module artifacts invocations of serviceloader::load methods, both slow , unreliable. module uses particular service fundamental aspect of module’s definition, both efficiency , clarity express in module’s declaration 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 fundamental module system know uses of particular service, how introduce efficiency? aren't services loaded lazily? why can't service loader providers on fly?

when jvm launches, module system resolves dependencies , builds module graph. modules make graph available @ run time (even if others observable). if modules decoupled via services, there chance, though, providing modules not transitive dependencies of initial module. without further efforts, service provider modules routinely not make module graph , not available @ run time when module tries use service.

in order java.sql module make use of driver [...] module system must add driver module module graph , resolve dependencies [...].

so services work, provider modules must make module graph if not transitively required initial module. how can module system identify modules needed service providers? use provides clause? little much. no, providers of services needed should resolved.

this makes necessary identify services uses. others have pointed out, bytecode analysis slow , unreliable, more explicit mechanism needed guarantee efficiency , correctness: uses clauses. them can module system reliably , efficiently make service provider modules available.

if application module, module declaration must have uses directive specifies service; helps locate providers , ensure execute reliably.

you can observe behavior if launching a service-based application flag --show-module-resolution:

root monitor monitor requires monitor.observer [...] monitor binds monitor.observer.beta monitor binds monitor.observer.alpha 

the module monitor binds modules monitor.observer.alpha , monitor.observer.beta though not depend on either of them.

(quotes the state of module system; emphasis mine.)


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -