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.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Unlocking Java, Spring, and Dubbo: How SPI Powers Extensible Architecture

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):

Configuration file
Configuration file

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.

ServiceLoader process
ServiceLoader process

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.

spring.factories example
spring.factories example

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 SPI config
Dubbo SPI config

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.

Java vs Spring vs Dubbo SPI comparison
Java vs Spring vs Dubbo SPI comparison
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaDubbospringSPIservice-provider-interface
Java Backend Technology
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.