开发者社区> 小龙猫> 正文

转 SPI和API的区别

简介:
+关注继续查看

以下内容来自: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 被框架扩张人员使用。

Java类库中的实例

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

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

如何实现这种结构?

1
2
3
4
5
6
7
8
public class Program {
    public static void main(String[] args) throws InstantiationException,
        IllegalAccessException, ClassNotFoundException {
        Class.forName("SpiA");
        Api api = new Api("a");
        api.Send("ABC");
    }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;
 
public class Api {
    private static HashMap<String, Class<? extends Spi>> spis = 
        new HashMap<String, Class<? extends Spi>>();
    private String protocol;
 
    public Api(String protocol) {
        this.protocol = protocol;
    }
 
    public void Send(String msg) throws InstantiationException,
        IllegalAccessException {
        Spi spi = spis.get(protocol).newInstance();
        spi.send("消息发送开始");
        spi.send(msg);
        spi.send("消息发送结束");
    }
 
    public static void Register(String protocol, Class<? extends Spi> cls) {
        spis.put(protocol, cls);
    }
}


1
2
3
public interface Spi {
    void send(String msg);
}

 

1
2
3
4
5
6
7
8
9
10
public class SpiA implements Spi {
    static {
        Api.Register("a", SpiA.class);
    }
 
    @Override
    public void send(String msg) {
        System.out.println("SpiA:" + msg);
    }
}

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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
ASP.NET Core Web API 帮助页
ASP.NET Core Web API 帮助页
78 0
一文概览设计Web API 中的细节
一文概览设计Web API 中的细节
52 0
借助 Web Animations API 实现一个鼠标跟随偏移动画
借助 Web Animations API 实现一个鼠标跟随偏移动画
58 0
WebAPI学习(一)——创建Web API程序
WebAPI学习(一)——创建Web API程序
111 0
几种不常用Web API(振动、重力感应、联网状态、系统电量信息、页面可见性、canvas转base64、监听屏幕旋转、元素全屏显示)
navigator对象中有一些不常用的API,以下主要介绍vibrate振动,deviceorientation重力感应,online联网状态,getBattery系统电量信息,visibilitychange页面可见性,toDataURL(canvas转base64),orientationchange监听屏幕旋转和fullScreen元素全屏显示。
92 0
.NET MVC第十章 vue axios解析web api接口
.NET MVC第十章 vue axios解析web api接口
37 0
.NET MVC第九章、Web Api Json序列化与反序列化
.NET MVC第九章、Web Api Json序列化与反序列化
57 0
.NET MVC第八章、Web Api 跨域接口
.NET MVC第八章、Web Api 跨域接口
56 0
【前端】【H5 API】Web存储 Web Storage
【前端】【H5 API】Web存储 Web Storage
81 0
每日一学—Web API之requestAnimationFrame
If you’ve never written code to animate inside the browser, you can stop reading :) ( ´・・)ノ(._.`)
97 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
CUDA Math API
立即下载
阿里云 API 精选手册(Alibaba Cloud API Playbook)
立即下载
重保场景及API安全指南
立即下载