Introduction to Java SPI (Service Provider Interface) __java

Source: Internet
Author: User
Introduction to SPI

The SPI is all called (Service Provider Interface), a service discovery mechanism built into the JDK.

A service usually refers to a known interface or abstract class, the service provider is the implementation of the interface or abstract class, and then stored in the resource path Meta-inf/services directory according to the SPI standard, the file is named the fully qualified name of the service interface. If you have a service interface:

Package Com.ricky.codelab.spi;

Public interface Demoservice {public

    string Sayhi (String msg);

Its service implementation classes are:

Package Com.ricky.codelab.spi.impl;

Import Com.ricky.codelab.spi.DemoService;

public class Demoserviceimpl implements Demoservice {

    @Override public
    string Sayhi (String msg) {return

        " Hello, "+MSG;
    }

}"

It needs to be in meta-inf/at this time Services to create a file named Com.ricky.codelab.spi.DemoService, where the content is the fully qualified name of the implementation class: Com.ricky.codelab.spi.impl.DemoServiceImpl.
If the service is implemented with multiple services, each line writes a service implementation (#后面的内容为注释), and the file can only be encoded in UTF-8.

Then, we can pass Serviceloader.load (class Class); To dynamically load the implementation class for the service.

Many development frameworks Use the Java SPI mechanism, such as Java.sql.Driver SPI implementations (MySQL-driven, Oracle-driven, etc.), common-logging log interface implementations, Dubbo extension implementations, and so on. the agreement of the SPI mechanism creates a file named in the meta-inf/services/directory with a fully qualified service interface, which contains the fully qualified name of the service interface, and the file encoding must be UTF-8. Use Serviceloader.load (class Class); Dynamically load the implementation class of the service interface. If the implementation class for the SPI is a jar, it needs to be placed under the classpath of the current program. The specific implementation class of the service must have a construction method with no parameters. Sample Development Environment JDK 1.7 IntelliJ Idea Maven 3.3.9 1, Project structure

2. Service Interface Definition

Package Com.ricky.codelab.spi;

Public interface Demoservice {public

    string Sayhi (String msg);
3, Service Interface implementation class

In this example, there are two implementation classes, Demoservice: Englishdemoserviceimpl and Chinesedemoserviceimpl, with the following code:
Englishdemoserviceimpl.java

Package Com.ricky.codelab.spi.impl;

Import Com.ricky.codelab.spi.DemoService;

public class Englishdemoserviceimpl implements Demoservice {

    @Override public
    string Sayhi (String msg) {

        Return "Hello," +msg
    }

}


Chinesedemoserviceimpl.java

Package Com.ricky.codelab.spi.impl;

Import Com.ricky.codelab.spi.DemoService;

public class Chinesedemoserviceimpl implements Demoservice {

    @Override public
    string Sayhi (String msg) { return

        "Hello," +msg;
    }


meta-inf/services/Configuration

Create the meta-inf/services/directory under Src/main/resources, and make a new Com.ricky.codelab.spi.DemoService file that reads as follows:

#English implementation
Com.ricky.codelab.spi.impl.EnglishDemoServiceImpl

#Chinese Implementation
Com.ricky.codelab.spi.impl.ChineseDemoServiceImpl
Load Service Implementation class
Import Java.util.Iterator;
Import Java.util.ServiceLoader;
Import Com.ricky.codelab.spi.DemoService;

serviceloader<demoservice> Serviceloader = Serviceloader.load (demoservice.class);
Iterator<demoservice> it = Serviceloader.iterator ();
while (It!=null && it.hasnext ()) {
     Demoservice demoservice = It.next ();
 System.out.println ("Class:" +demoservice.getclass (). GetName () + "* *" +demoservice.sayhi ("World"));
}

Run Result:

Class:com.ricky.codelab.spi.impl.demoserviceimpl***hello, World
Hello, class:com.ricky.codelab.spi.impl.chinesedemoserviceimpl***.


code Download

Code has been uploaded to GitHub, Link: https://github.com/FBing/java-SPI-samples

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.