Java SPI mechanism and usage examples

Source: Internet
Author: User

Introduction to JAVA SPI

The SPI is a service load method provided by Java, with the full name of the services Provider Interface. According to the Java SPI specification, we can define a service interface, where the implementation is provided by the corresponding implementation provider, i.e. the service provider. Then in the use of the SPI specification to obtain the corresponding service provider service implementation. Registration and discovery of services through the SPI service loading mechanism effectively avoids writing service providers to death in code. Thus, the decoupling between modules can be realized based on interface programming.

Conventions of the SPI mechanism

1 Create a file in the meta-inf/services/directory named after the interface fully qualified name, which is the fully qualified name of the API-specific implementation class

2 Implementing classes in dynamic load Meta-inf using the Serviceloader class

3 if the implementation class for the SPI is a Jar, it needs to be placed in the main program ClassPath

4 API Specific implementation classes must have a constructor with no parameters

Example of SPI application scenario JDBC

Jdbc4.0 Previously, developers also needed to load the driver based on class.forname ("xxx"), JDBC4 also found the driver provider based on the SPI mechanism, which can be metainf/services/ The Java.sql.Driver file specifies the way the implementation class is exposed to the driver provider.

common-logging

Apache provides the earliest log façade interface. Only interfaces, not implemented. The specific scenario is implemented by each provider, and the discovery log provider finds the log-industry implementation class by scanning the metainf/services/org.apache.commons.logging.logfactory configuration file and reading the contents of the file. As long as our log implementation contains this file, and in the file to develop the Logfactory factory interface implementation class can be.

SPI Mechanism code example

Interface People.java

 Package org.louis.spi.test;  Public Interface people {
Public String speak ();
}

Implementing Class Chinese.java

Package publicclassimplements  people{    public   speak () {         return "Chinese speak Chinese." ;     } }

Implementing Class American.java

Package org.louis.spi.test, public class American implements people{    speak () {         Return "American speak 中文版." ;     } }

Create a file to be placed in the Meta-inf/services directory:

File name: org.louis.spi.test.People

File contents:

Org.louis.spi.test.Chineseorg.louis.spi.test.American

Create a new test project that introduces the jar package generated by the above code

Test class Spitest.java

 Packageorg.louis.test;ImportJava.util.Iterator;ImportJava.util.ServiceLoader;ImportOrg.louis.spi.test.People;
ImportOrg.louis.spi.test.Chinese;
ImportOrg.louis.spi.test.American;
Public classSpitest { Public Static voidMain (string[] args) {
Serviceloader<People> peoples = serviceloader.load (people.class);
Iterator<IOperation> iterator =Peoples.iterator ();
while(Iterator.hasnext ()) {people people=Iterator.next (); System.out.println (People.speak ()); } } }

Operation Result:

Chinese speak Chinese.

American speak 中文版.

From the above example, we see that if I want to join a new Korean implementation, it is only necessary to create a new project, the creation of a Koeran class implementation of the People interface, and in their own project Meta-inf/services directory placement of a configuration file to specify the Koeran implementation class, The development of a new service implementation is accomplished by hitting the project into a jar package.

Westerly Pony
Source: https://www.cnblogs.com/xifengxiaoma/
All rights reserved, welcome reprint, Reprint please indicate the original author and source.

Java SPI mechanism and usage examples

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.