以下内容来自: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 实现的加载可以使用很多种方式,文中是最基本的方式。

转 SPI和API的区别相关推荐

  1. java spi与api,SPI和API之间的区别?

    More specifically, for Java libraries, what makes them an API and/or SPI? 解决方案the API is the descrip ...

  2. c运行库、c标准库、windows API的区别和联系

    c运行库.c标准库.windows API的区别和联系 C运行时库函数 C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.    API函数 API函数是操作系统为方便用户设计应用 ...

  3. ABI 与 API 的区别(应用程序二进制接口、应用程序编程接口)

    文章目录 1.ABI 简介 2.API 简介 3.ABI 与 API 的区别 1.ABI 简介 C++ 可执行文件是由编译器将源代码编译成目标文件后链接生成的,那么如果目标文件由不同的编译器编译生成, ...

  4. remote API和regular API 的区别

    问题: socket 阻塞 非阻塞 同步 异步 verp remote API和regular API 的区别 remote API 什么是remote API: V-REP offers a rem ...

  5. Android各个版本API的区别

    Android各个版本API的区别 Android 1.5 API变更概要: 1.UI framework · Framework for easier background/UI thread in ...

  6. Micropython——关于I2C和SoftI2C以及SPI和SoftSPI的区别

    首先,我们先看下面关于Raspberry Pi Pico的一张引脚图 1.关于I2C和SoftI2C的区别和联系 能够清晰地看到上面框图所圈起来的就是我们常常所说的I2C引脚接口,这里的接口指的就是硬 ...

  7. SDK和API的区别

    SDK和API的区别 一.API的概念 二.SDK的概念 三.SDK和API的区别 在正式了解它们的区别之前,我们得先知道 SDK 和 API 分别是什么? 一.API的概念 我们先来看这样一张图: ...

  8. ABI 与 API 的区别

    文章目录 1.ABI 简介 2.API 简介 3.ABI 与 API 的区别 参考文献 1.ABI 简介 C++ 可执行文件是由编译器将源代码编译成目标文件后链接生成的,那么如果目标文件由不同的编译器 ...

  9. quad spi 和qpi的区别

    从用法上看,Quad SPI与QPI的区别在于指令输入的方式,Quad SPI模式指令是通过SI(IO0) pin输入的,地址,数据是通过SI(IO0)/SO(IO1)/IO2/IO3传输的,QPI模 ...

最新文章

  1. 任命新CFO 百度迎来首位女性高管
  2. bootstrap基础学习四篇
  3. 解决:XCODE 4.2 无法连接SVN,提示:Unable to load revisions
  4. Linux学习笔记之——Linux硬盘分区知识
  5. hotmail在outlook2007中的设置
  6. spring 基于xml方式配置aop
  7. 为什么换工作?(面试必问问题)
  8. avalon视频学习笔记(六)
  9. 哪个行业既锻炼人又便于今后创业?
  10. 如何加精准粉丝,一个巧妙实用的方法!
  11. Rainbond 5.1.3 发布,快速部署和运维 Spring Cloud 集群
  12. 凸优化第三章凸函数 3.6 关于广义不等式的凸性
  13. C语言程序设计学习心得体会总结
  14. 计算机上word如何批量打印,批量打印文件夹中的多个Word文档
  15. Python特定场景数据内型“解压”操作
  16. python火车抢票_春运火车票太难抢?不如试试这个python开源工具吧
  17. 移动魔百盒CM311-1sa_ZG代工_S905L3A 安卓9.0 鸿蒙动画_线刷固件包
  18. 羊城杯2022--Writeup
  19. 学会在Android Studio里使用列表视图制作通讯录
  20. centos7查看udp端口_CentOS7查看开放端口命令及开放端口号

热门文章

  1. C++编程问题--glibc detected *** ./a.out: munmap_chunk(): invalid pointer: xxxxxx
  2. 开课吧Java课堂:线程间是如何实现通信
  3. svn添加提交备注限制和自动发布web项目
  4. centos7 安装 php7
  5. form表单file,select选择后自动提交
  6. java使用smartupload组件实现文件上传的方法
  7. SpriteBuilder切换解决方案以及CCB的修改与保存
  8. 淘宝店铺类目怎么删除
  9. [Step By Step]SAP HANA PAL多项式回归预测分析Polynomial Regression编程实例FORECASTWITHPOLYNOMIALR(预测)...
  10. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal