CKTIS / Java
The keyword provides
The keyword provides: The provides S with P directive in module-info declares that a module supplies service S through provider class P.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
providesThe provides S with P directive in module-info declares that a module supplies service S through provider class P.
Combine it with uses and ServiceLoader so consumers discover implementations without depending on concrete classes.
A provider must be a public top-level class or interface, or a public static member class or interface, in the current module. A public static no-argument provider() method must return a subtype of the service; without it, the provider itself must be a service subtype with a public no-argument constructor.
Several providers may be declared, but ServiceLoader guarantees neither a global order nor a single instance.
The descriptor links service=demo.Service with providers=[demo.Fast, demo.Safe].
Examples
import java.lang.module.*; public class Main { public static void main(String[] a){var d=ModuleDescriptor.newModule("demo.provider").provides("demo.Service",java.util.List.of("demo.Fast","demo.Safe")).build();var p=d.provides().iterator().next();System.out.println("service="+p.service());System.out.println("providers="+p.providers());} }Result
service=demo.Service providers=[demo.Fast, demo.Safe]
The descriptor links service=demo.Service with providers=[demo.Fast, demo.Safe].