How to use Dubbo SPI

Opening chapter

As mentioned earlier Introduction and usage of Java SPI , and the Dubbo SPI to be mentioned in this article is an upgrade and transformation based on Java SPI (some shortcomings have been improved and several new playing methods have been added). See the official website for details Dubbo SPI part.

This article mainly talks about some usage methods of Dubbo SPI.

In addition, the following articles will supplement other uses of Dubbo SPI and the source code parsing of Dubbo SPI.

Of course Official website There are also instructions and source guide for Dubbo SPI, which you can choose.


The usage of Dubbo SPI is divided into the following parts:

  • Basic use
  • Automatic packaging of extension point
  • Extended point adaption
  • Automatic assembly of extension points
  • Automatic activation of extension point

This article first introduces the first two methods of use. It is suggested to take a look at them first Introduction and usage of Java SPI , understand the mechanism and use of SPI.

1. Basic use

From this point of view, Dubbo SPI is not very different from JAVA SPI in use.

The main differences are:

The SPI of the JDK standard instantiates all the implementations of extension points at one time. If there is an extension implementation initialization, it will take a long time, but if it is not used, it will also load, which will waste resources.

Dubbo SPI can optionally instantiate an implementation.

Here are the differences in writing methods. The specific codes are as follows:

1.1 define an interface

// Difference 1: Dubbo needs SPI annotation
public interface HelloService {

    void sayHello(String name);


1.2 define the implementation class of the interface

public class HelloServiceImpl implements HelloService {
    public void sayHello(String name) {
        System.out.println("hello, " + name);

1.3 create the following file under classpath

# Difference 2: different folder naming methods and different file content formats
META-INF\dubbo\com.nimo.service.HelloService (Named after the full path of the interface name)

# The file content is in the form of key value

## There can be multiple implementations

1.4 program entry

public class App 
    public static void main( String[] args )
        // Difference 3: different API
        ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);
        // Obtain the corresponding instantiated object by specifying the extension class name y
        // Here, the extensionLoader can use it as a map
        HelloService helloService = extensionLoader.getExtension("helloService");

Operation result:

hello, xiaoming

In fact, there are a lot of concurrent HashMap containers in the extension loader, which perform their respective duties. The specific source code chapter will elaborate later.

2. Automatic packaging of extension point

When the ExtensionLoader loads the extension point, if the extension point loaded has a copy constructor, it is determined as the extension point Wrapper class.

This is a bit similar to AOP. You can see the specific code and effect.

2.1 add a new class

// Implement the extended interface and the construction method of the following template
public class HelloServiceImplWrapper implements HelloService {

    private HelloService helloService;

    public HelloServiceImplWrapper(HelloService helloService){
        this.helloService = helloService;

    public void sayHello(String name) {
        // This is equivalent to the enhancement of the original Service

2.2 modify the contents of META-INF\dubbo\com.nimo.service.HelloService file

// The second step is to add part of the original content, and change it as follows:

Operation result:

hello, xiaoming


This paper first introduces the basic use of Dubbo SPI, and then introduces one of the advanced play methods - automatic packaging of extension points, Its function is similar to an AOP, as the official website says.

In addition, when obtaining the specific instance of the implementation class of the extension point through ExtensionLoader, the approximate pseudo code is as follows:

HelloService helloService = new HelloServiceImpl();
if (wrapper existence) {
   helloService = wrapper; 
return helloService;

The last call to the sayHello method is called wrapper.sayHello().

Tags: Dubbo Java JDK

Posted on Wed, 25 Mar 2020 09:34:06 -0700 by ehmer