Unlocking Java, Spring, and Dubbo: How SPI Powers Extensible Architecture
SPI (Service Provider Interface) is a service discovery mechanism used in Java, Spring, and Dubbo that loads implementations via configuration files, enabling runtime extensibility, decoupling, and plug‑in architecture, with examples ranging from custom interfaces to MySQL drivers and Dubbo extensions.
What is SPI?
SPI (Service Provider Interface) is a service discovery mechanism that loads implementations of an interface at runtime by reading configuration files containing the fully‑qualified class names.
Java SPI
Java SPI combines interface‑based programming, the Strategy pattern, and configuration files. The configuration file resides under classpath:/META-INF/services/ with the interface’s fully‑qualified name as the file name and the implementation class names as its content.
Example code:
// Interface
public interface Superman {
void introduce();
}
// Implementation 1
public class IronMan implements Superman {
@Override
public void introduce() {
System.out.println("I am Iron Man!");
}
}
// Implementation 2
public class CaptainAmerica implements Superman {
@Override
public void introduce() {
System.out.println("I am Captain America!");
}
}Configuration file (META‑INF/services/com.example.Superman):
Test code:
public static void main(String[] args) {
ServiceLoader<Superman> loader = ServiceLoader.load(Superman.class);
System.out.println("Java SPI:");
loader.forEach(Superman::introduce);
}Running the program prints the messages from both implementations.
Java SQL Driver Example
The MySQL connector JAR uses SPI to provide an implementation of java.sql.Driver, allowing the driver to be discovered and loaded automatically at runtime.
Java SPI Internals
The core logic resides in java.util.ServiceLoader, which scans the classpath for META‑INF/services/ files, caches the mappings, and iterates over the discovered providers.
Spring SPI
Spring’s SPI is similar to JDK SPI but uses classpath:/META-INF/spring.factories. The file is a key=value list where the key is the interface name and the value is one or more implementation class names.
Dubbo SPI
Dubbo extends the SPI concept. Each extension point (e.g., org.apache.dubbo.rpc.Filter, Protocol, LoadBalance) has its own configuration file under META‑INF/dubbo/. The file contains alias=implementationClass pairs, supporting alias lookup and reducing unnecessary instantiation.
Dubbo’s ExtensionLoader reads these files, caches the mappings, and provides methods such as ExtensionLoader.getExtensionLoader(Filter.class) to obtain specific extensions.
Comparison
All three frameworks share the same principle of decoupling interface definitions from implementations via configuration files, but they differ in file locations, format, and additional features like alias support in Dubbo.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
