The SPI mechanism of Java with a simple example

Source: Internet
Author: User

One, SPI mechanism

Here is a concept of SPI, SPI English for service Provider interface is literally understood as the service provider interface, as in the name of the SPI to understand the SPI is the service provider interface My definition of SPI: an interface that is available to developers of service providers and extended framework functions.

In our daily development is to abstract the problem into the API and then provide the implementation of various APIs, the implementation of these APIs are encapsulated with our jar or framework, although when we want to provide a new implementation of the API can not modify the original code can only implement the API to provide a new implementation of the API, But we're still building a new jar or framework (although it's possible to scan a directory for a new implementation of the API's loaded APIs, but this is not a Java mechanism, it's just the Hack method), and with the Java SPI mechanism we can provide a new implementation for the API without modifying the jar package or framework.

Many frameworks use Java's SPI mechanism, such as Java.sql.Driver's SPI implementation (MySQL driver, Oracle driver, etc.), common-logging log interface Implementation, Dubbo extension implementation, and so on.

SPI the Convention of the Mechanism:

1) Create a file in the meta-inf/services/directory named after the interface fully qualified name the full qualified name of the API-specific implementation class for the file content

2) using the Serviceloader class to dynamically load the implementation class in the Meta-inf

3) If the SPI implementation class is a jar, it needs to be placed in the main program Classpath

4) API specific implementation class must have a constructor without parameters

SPI mechanism structure diagram

Second, SPI Mechanism Example

Instance structure diagram

ioperation Interface:

/** * Created by LX on 2015/3/8. */public interface IOperation {public    int operation (int numbera, int numberb);}

Plusoperationimpl implementation:

Import co.solinx.demo.api.ioperation;/** * Created by LX on 2015/3/8. */public class Plusoperationimpl implements IOperation {    @Override public    int operation (int numbera, int numberb {        return numbera + Numberb;    }}

SPI implementation class for the interface: Divisionoperationimpl

Import co.solinx.demo.api.ioperation;/** * Created by LX on 2015/3/8. */public class Divisionoperationimpl implements IOperation {    @Override public    int operation (int numbera, int Numberb) {        return numbera/numberb;    }}

meta-inf/services files in the directory:

File name: co.solinx.demo.api.IOperation, Content: Co.solinx.demo.spi.DivisionOperationImpl

Main class:

import Co.solinx.demo.api.ioperation;import Co.solinx.demo.impl.plusoperationimpl;import Co.solinx.demo.spi.divisionoperationimpl;import Java.util.Iterator; Import java.util.serviceloader;/** * Created by LX on 2015/3/8.        */public class Main {public static void main (string[] args) {ioperation plus = new Plusoperationimpl ();        IOperation Division = new Divisionoperationimpl ();        System.out.println (Plus.operation (5, 3));        System.out.println (Division.operation (9, 3));        serviceloader<ioperation> operations = Serviceloader.load (Ioperation.class);        iterator<ioperation> operationiterator = Operations.iterator ();        System.out.println ("ClassPath:" +system.getproperty ("Java.class.path"));            while (Operationiterator.hasnext ()) {ioperation operation = Operationiterator.next ();        System.out.println (Operation.operation (6, 3)); }    }}

Operation Result:

if the SPI implementing packaging as a jar need to put the jar put it in Classpath . directory, the SPI jar package Run Result:

Article starting address: Solinx

http://www.solinx.co/archives/142

The SPI mechanism of Java with a simple example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.