1. Dubbo's JDK introduction to SPI

Thoughts of Dubbo SPI --->! Are you familiar with the spi (service provider interface) mechanism, that is, we define the service interface standard and let the manufacturer implement it (if you don't understand spi, please Google Baidu),

    JDK implements the service lookup function of the spi mechanism through the ServiceLoader class.

Let's take a look at how jdk is implemented:         JDK implements SPI service lookup: ServiceLoader.

package com.test;

public interface SPI {

       String sayHello();

}

ServiceLoader will traverse all jars looking for META-INF/services/com.test.SPI file

Then various manufacturers provide implementation

 

package com.test.testA;

public class SPITestA implements SPI {

    

   public String syaHello() {

         return “hello---->testA”;

   }

}

 

The contents of the META-INF/services/com.test.SPI file in the jar package provided by the testA manufacturer are:

com.test.testA.SPITestA #Manufacturer A's spi implementation full path class name

 

Similar to other manufacturers.

jdk ServiceLoader.load(Spi.class) reads testA, etc. to provide files in the jar package. ServiceLoader implements the Iterable interface and can traverse all implementations through the while for loop statement.

Multiple implementations of an interface provide the implementation of strategies just like the strategy pattern.

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403275&siteId=291194637
Recommended