SPI和service provider framework介绍

来源:互联网 发布:addlistener js 编辑:程序博客网 时间:2024/05/18 03:52

我的理解:A设计了一个框架,并且实现了大部分功能,并提供了让B调用的接口即API。但是,A实现的框架中,某些部分必须依赖于B的具体场景,但B只是使用者,没必要研究怎么补全A的框架,此时,服务提供商C,提供了这部分的实现。

         即,用户B如果要正常使用A提供的API,那么必须保证A框架时完整的,就必须将A没有实现的部分用C的实现来补全。这里的A无法实现的要让C来补全的部分,通过接口形式表现出来,即SPI。



以下内容来自:http://blog.csdn.net/mosquitolxw/article/details/25290315

What is the difference between Service Provider Interface (SPI) and Application Programming Interface (API)?

More specifically, for Java libraries, what makes them an API and/or SPI?

the API is the description of classes/interfaces/methods/... that you call and use to achieve a goal

the SPI is the description of classes/interfaces/methods/... that you extend and implement to achieve a goal

Put differently, the API tells you what a specific class/method does for you and the SPI tells you what you must do to conform.

Sometimes SPI and API overlap. For example in JDBC the Driver class is part of the SPI: If you simply want to use JDBC, you don't need to use it directly, but everyone who implements a JDBC driver must implement that class.

The Connection interface on the other hand is both SPI and API: You use it routinely when you use a JDBC driver and it needs to be implemented by the developer of the JDBC driver.


以下内容来自:http://www.cnblogs.com/happyframework/p/3349087.html

背景

Java 中区分 Api 和 Spi,通俗的讲:Api 和 Spi 都是相对的概念,他们的差别只在语义上,Api 直接被应用开发人员使用,Spi 被框架扩张人员使用,详细内容可以看:http://www.cnblogs.com/happyframework/p/3325560.html。

Java类库中的实例

代码

复制代码
1         Class.forName("com.mysql.jdbc.Driver");2         Connection conn = DriverManager.getConnection(3                 "jdbc:mysql://localhost:3306/test", "root", "123456");4         Statement stmt = conn.createStatement();5 6         ResultSet rs = stmt.executeQuery("select * from Users");
复制代码

说明

java.sql.Driver 是 Spi,com.mysql.jdbc.Driver 是 Spi 实现,其它的都是 Api。

如何实现这种结构?

代码

复制代码
 1 public class Program { 2  3     public static void main(String[] args) throws InstantiationException, 4             IllegalAccessException, ClassNotFoundException { 5         Class.forName("SpiA"); 6  7         Api api = new Api("a"); 8         api.Send("段光伟"); 9     }10 }
复制代码
复制代码
 1 import java.util.*; 2  3 public class Api { 4     private static HashMap<String, Class<? extends Spi>> spis = new HashMap<String, Class<? extends Spi>>(); 5     private String protocol; 6  7     public Api(String protocol) { 8         this.protocol = protocol; 9     }10 11     public void Send(String msg) throws InstantiationException,12             IllegalAccessException {13         Spi spi = spis.get(protocol).newInstance();14 15         spi.send("消息发送开始");16         spi.send(msg);17         spi.send("消息发送结束");18     }19 20     public static void Register(String protocol, Class<? extends Spi> cls) {21         spis.put(protocol, cls);22     }23 }
复制代码
1 public interface Spi {2     void send(String msg);3 }
复制代码
 1 public class SpiA implements Spi { 2     static { 3         Api.Register("a", SpiA.class); 4     } 5  6     @Override 7     public void send(String msg) { 8         System.out.println("SpiA:" + msg); 9     }10 11 }
复制代码

说明

Spi 实现的加载可以使用很多种方式,文中是最基本的方式。


以下内容来自:http://stackoverflow.com/questions/2954372/difference-between-spi-and-api

From Effective Java 2nd Edition:

A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations.

There are three essential components of a service provider framework: a service interface, which providers implement; a provider registration API, which the system uses to register implementations, giving clients access to them; and a service access API, which clients use to obtain an instance of the service. The service access API typically allows but does not require the client to specify some criteria for choosing a provider. In the absence of such a specification, the API returns an instance of a default implementation. The service access API is the “flexible static factory” that forms the basis of the service provider framework.

An optional fourth component of a service provider framework is a service provider interface, which providers implement to create instances of their service implementation. In the absence of a service provider interface, implementations are registered by class name and instantiated reflectively (Item 53). In the case of JDBC, Connection plays the part of the service interface, DriverManager.registerDriver is the provider registration API, DriverManager.getConnection is the service access API, and Driver is the service provider interface.

There are numerous variants of the service provider framework pattern. For example, the service access API can return a richer service interface than the one required of the provider, using the Adapter pattern [Gamma95, p. 139]. Here is a simple implementation with a service provider interface and a default provider:

// Service provider framework sketch// Service interfacepublic interface Service {    ... // Service-specific methods go here}// Service provider interfacepublic interface Provider {    Service newService();}// Noninstantiable class for service registration and accesspublic class Services {    private Services() { }  // Prevents instantiation (Item 4)    // Maps service names to services    private static final Map<String, Provider> providers =        new ConcurrentHashMap<String, Provider>();    public static final String DEFAULT_PROVIDER_NAME = "<def>";    // Provider registration API    public static void registerDefaultProvider(Provider p) {        registerProvider(DEFAULT_PROVIDER_NAME, p);    }    public static void registerProvider(String name, Provider p){        providers.put(name, p);    }    // Service access API    public static Service newInstance() {        return newInstance(DEFAULT_PROVIDER_NAME);    }    public static Service newInstance(String name) {        Provider p = providers.get(name);        if (p == null)            throw new IllegalArgumentException(                "No provider registered with name: " + name);        return p.newService();    }}



0 0