Understanding Java SPI Mechanism and Its Application in Dubbo
This article explains the design goals and conventions of Java's Service Provider Interface (SPI), demonstrates a practical JDK SPI example with a Command interface, discusses its drawbacks, and shows how Dubbo extends and customizes SPI for protocol implementations.
1. Java SPI Design Goals In modular Java design, modules should depend on interfaces rather than concrete implementations, allowing interchangeable implementations without hard‑coding. The SPI mechanism provides a way to discover service implementations at runtime, similar to IoC, by locating provider classes via configuration files.
1.2 SPI Conventions Providers place a file named after the service interface inside META-INF/services/ within the JAR. The file lists fully‑qualified implementation class names, which the runtime reads to instantiate the appropriate service.
Examples include the java.sql.Driver files for MySQL and Oracle drivers and the javax.servlet.ServletContainerInitializer file for Spring‑Web.
1.3 JDK SPI Practical Example
Define an interface Command:
public interface Command {
void execute();
}Provide two implementations TurnOnCommand and TurnOffCommand:
public class TurnOnCommand implements Command {
public void execute() {
System.out.println("Turn on....");
}
}
public class TurnOffCommand implements Command {
public void execute() {
System.out.println("Turn off....");
}
}Create a service configuration file resources/META-INF/services/com.abc.spi.Command containing:
com.abc.spi.TurnOnCommand
com.abc.spi.TurnOffCommandWrite a test class that loads the services via ServiceLoader:
public class Test {
public static void main(String[] args) {
ServiceLoader<Command> loader = ServiceLoader.load(Command.class);
for (Command cmd : loader) {
cmd.execute();
}
}
}The test output shows both commands being executed.
1.4 Drawbacks of JDK SPI The standard SPI eagerly instantiates all implementations, which can waste resources if some are heavy and unused. It also lacks built‑in IoC and AOP support, prompting frameworks like Dubbo to implement their own enhanced SPI.
2. Dubbo SPI Conventions
Dubbo stores its SPI files under dubbo‑<version>.jar!\META-INF\dubbo\internal. Each file is named after the fully‑qualified interface (e.g., com.alibaba.dubbo.rpc.Protocol) and contains lines of the form extensionName=implementationClass.
3. Extending Dubbo SPI
To add a custom protocol:
Create a file
src/main/resources/META-INF/dubbo/com.apache.dubbo.rpc.Protocolwith a line like xxProtocol=com.abc.XxxProtocol.
Implement the protocol class:
package com.xxx;
import org.apache.dubbo.rpc.Protocol;
public class XxxProtocol implements Protocol {
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
return new XxxExporter(invoker);
}
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
return new XxxInvoker(type, url);
}
}Provide supporting classes:
package com.xxx;
import org.apache.dubbo.rpc.support.AbstractExporter;
public class XxxExporter<T> extends AbstractExporter<T> {
public XxxExporter(Invoker<T> invoker) throws RemotingException {
super(invoker);
// ...
}
public void unexport() {
super.unexport();
// ...
}
} package com.xxx;
import org.apache.dubbo.rpc.support.AbstractInvoker;
public class XxxInvoker<T> extends AbstractInvoker<T> {
public XxxInvoker(Class<T> type, URL url) throws RemotingException {
super(type, url);
}
protected Object doInvoke(Invocation invocation) throws Throwable {
// ...
}
}Package the JAR and add it as a dependency to the Dubbo provider project, then configure the protocol in Spring XML: <dubbo:protocol name="XxxProtocol" port="20000"/> The article concludes with a brief thank‑you note and source attribution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
