Analysis of the SPI mechanism in Java

Source: Internet
Author: User

1 introduction of SPI mechanism

The full name of the SPI is the service Provider Interface. Most developers may be unfamiliar because this is for vendors or plug-ins. A more detailed description is available in the Java.util.ServiceLoader documentation. Simply summarize the idea of the Java SPI mechanism. The abstract modules in our system often have many different implementations, such as the program of the log module, the XML parsing module, the JDBC module scheme, etc. Object-oriented design, we generally recommend that the module is based on interface programming between modules do not hard-code implementation classes. Once a specific implementation class is involved in the code, the pluggable principle is violated, and if you need to replace an implementation, you need to modify the code. This requires a service discovery mechanism in order to realize that the module can not be dynamically specified in the program when it is assembled. The Java SPI provides a mechanism for finding a service implementation for an interface. A bit like the idea of the IOC is to move control of the assembly outside the program, which is particularly important in modular design.

2 SPI specific Agreement

The specific convention of the Java SPI is that when the provider of the service provides an implementation of the service interface, a file named after the service interface is created in the meta-inf/services/directory of the jar package. The file is a concrete implementation class that implements the service interface. When the external program assembles this module, it can find the specific implementation class name through the configuration file in the jar package meta-inf/services/, and load the instantiation to complete the injection of the module. Based on such a convention, you can find the implementation class of the service interface well, without needing to make it in code. JDK provides a service implementation lookup of a tool class: Java.util.ServiceLoader

3 Application Scenarios

1.common-logging
The first façade interface for the log provided by Apache. Only interfaces, not implemented. The specific scenario is implemented by each provider, and the discovery log provider scans the Meta-inf/services/org.apache.commons.logging.logfactory configuration file to find the log to the business implementation class by reading the contents of the file. As long as our log implementation contains this file, and in the file to develop Logfactory factory interface implementation class can be.

2.jdbc

Before jdbc4.0, developers also needed to load drivers based on class.forname ("xxx"), JDBC4 also based on the SPI mechanism to discover driver providers that could be passed meta-inf/services/ Java.sql.Driver to expose the driver provider by specifying the implementation class in the file.

4 Case Description

A content management system has a search module. is based on interface programming. The implementation of the search may be based on a file system search, or it may be a database based search

The interface is defined as follows

Package My.xyz.spi;  
Import java.util.List;  
Public interface Search {public  
   List serch (String keyword);  
}

A company uses the file system search way to achieve the search interface, B Company adopted a database system to achieve the search interface

A company implementation of class COM. A.spi.impl.filesearch

B Company implementation of class COM. B.spi.impl.databasesearch

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

When a company publishes the implementation jar package, it will write the following in the jar package Meta-inf/services/my.xyz.spi.search file

Com. A.spi.impl.filesearch

B Company to publish the implementation jar package, then in the jar package in the Meta-inf/services/my.xyz.spi.search file to write down the following content

Com. B.spi.impl.databasesearch

Package com.xyz.factory;  
Import Java.util.Iterator;  
Import Java.util.ServiceLoader;  
Import My.xyz.spi.Search;  
public class Searchfactory {  
    private searchfactory () {  
    } public  
    static Search NewSearch () {  
        Search search = null;  
        serviceloader<search> Serviceloader = Serviceloader.load (search.class);  
        iterator<search> searchs = Serviceloader.iterator ();  
        if (Searchs.hasnext ()) {  
            search = Searchs.next ();  
        }  
        return search;  
    }  
Package my.xyz.test;  
Import Java.util.Iterator;  
Import Java.util.ServiceLoader;  
Import com.xyz.factory.SearchFactory;  
Import My.xyz.spi.Search;  
public class Searchtest {public  
    static void Main (string[] args) {  
        Search search = Searchfactory.newsearch (); 
  search.serch ("Java SPI Test");  
    }  

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.