Sub topic 12
Decoupling with services: uses and provides
This chapter assumes that you understand packages, interfaces, module-info.java, and basic javac and java commands. You should also know that a module must read another module with requires before it can use that module's exported types.
A service is an interface that describes an extension point. A service provider is a module that supplies an implementation of that interface. The consumer depends on the interface, but it does not name a particular implementation module.
The service relationship
Suppose an application needs a greeting. The API module defines the service interface:
// src/com.example.greeter.api/module-info.java
module com.example.greeter.api {
exports com.example.greeter.api;
}
// src/com.example.greeter.api/com/example/greeter/api/GreetingProvider.java
package com.example.greeter.api;
public interface GreetingProvider {
String greet(String name);
}A provider module implements the interface and registers its implementation with provides:
// src/com.example.greeter.french/module-info.java
module com.example.greeter.french {
requires com.example.greeter.api;
provides com.example.greeter.api.GreetingProvider
with com.example.greeter.french.FrenchGreeting;
}
// src/com.example.greeter.french/com/example/greeter/french/FrenchGreeting.java
package com.example.greeter.french;
import com.example.greeter.api.GreetingProvider;
public final class FrenchGreeting implements GreetingProvider {
public FrenchGreeting() {
}
@Override
public String greet(String name) {
return "Bonjour, " + name;
}
}The application declares uses and loads providers with ServiceLoader:
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.greeter.api;
uses com.example.greeter.api.GreetingProvider;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.greeter.api.GreetingProvider;
import java.util.ServiceLoader;
public class Main {
public static void main(String[] args) {
ServiceLoader<GreetingProvider> providers =
ServiceLoader.load(GreetingProvider.class);
for (GreetingProvider provider : providers) {
System.out.println(provider.greet("modules"));
}
}
}Compile and run all three modules:
javac -d out --module-source-path src \
-m com.example.greeter.api,com.example.greeter.french,com.example.app
java --module-path out \
--add-modules com.example.greeter.french \
-m com.example.app/com.example.app.MainThe application requires only the API module. It does not require com.example.greeter.french. This allows a different provider to be added without recompiling the application.
| Directive | Declared by | Purpose |
|---|---|---|
| `uses` | Consumer module | Announces that code loads a service |
| `provides ... with` | Provider module | Registers an implementation |
| `requires` | Consumer or provider | Makes the service interface readable |
If no provider is present, ServiceLoader can return an empty sequence. If a provider has an inaccessible constructor or does not implement the service correctly, discovery can fail with ServiceConfigurationError.
Exercise
Why does the application require the API module but not the French provider module?
Show answer
The application directly uses the exported service interface, while ServiceLoader discovers provider modules from the module graph at run time.