SPI implementation of Java extension mechanism

1, What is SPI

SPI (Service Provider Interface) is a mechanism that separates service interface and service implementation to achieve decoupling and greatly improves program scalability. The introduction of service provider is the implementer of SPI interface. Through local registration discovery, specific implementation classes can be obtained, which is easy to plug.

Scenario: a typical scenario is the process of loading drivers in JDBC.

2, Using demo

demo engineering structure:

 

1) First, we define a three-party package SpiInterface that provides interfaces

public interface SpiInterface {

    public void method();
}

2) Then we define two tripartite implementations of SpiIml01 and SpiIml02 to implement SpiInterface interface;
1,SpiIml01

public class SpiIml01 implements SpiInterface {

    public void method() {
        System.out.println("print SpiIml01");
    }
}

pom: introduce three-party interface package SpiInterface

<dependencies>
        <dependency>
            <groupId>com.robin</groupId>
            <artifactId>SpiInterface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

2,SpiIml02

public class SpiIml02 implements SpiInterface {

    public void method() {
        System.out.println("print SpiIml02");
    }
}

pom: introduce three-party interface package SpiInterface

<dependencies>
        <dependency>
            <groupId>com.robin</groupId>
            <artifactId>SpiInterface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

 

3) And for implementation projects, create a file named "interface fully qualified name" under the META-INF/services directory of each project, and the content is the fully qualified name of the implementation class;

 

4) Define test project SpiDemo

SpiDemo

public class SpiDemo {


    public static void main(String[] args) {
        ServiceLoader<SpiInterface> load = ServiceLoader.load(SpiInterface.class);
        Iterator<SpiInterface> iterator = load.iterator();
        while (iterator.hasNext()) {
            SpiInterface next = iterator.next();
            next.method();
        }
    }
}

pom: introduction of three-party interface package and implementation package

<dependencies>
        <dependency>
            <groupId>com.robin</groupId>
            <artifactId>SpiIml02</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.robin</groupId>
            <artifactId>SpiInterface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.robin</groupId>
            <artifactId>SpiIml01</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Run the main method: from the result, we can see that two implementation classes have been acquired; the order of acquisition depends on the order of your maven introduction and who gets first

Tags: snapshot JDBC Maven

Posted on Wed, 06 May 2020 07:15:32 -0700 by dessolator