当前位置:网站首页>SPI 与 API的区别
SPI 与 API的区别
2022-06-22 11:23:00 【JackieZhengChina】
背景
Java 中区分 API 和 SPI,通俗的讲:API 和 SPI 都是相对的概念,他们的差别只在语义上,API 直接被应用开发人员使用,SPI 被框架扩展人员使用
理解
API (Application Programming Interface)
- 大多数情况下,都是实现方来制定接口并完成对接口的不同实现,调用方仅仅依赖却无权选择不同实现。
SPI (Service Provider Interface)
- 而如果是调用方来制定接口,实现方来针对接口来实现不同的实现。调用方来选择自己需要的实现方。
从面向接口编程说起

当我们选择在调用方 和 实现方 中间引入 接口。上图没有给出“接口”应该位于哪个“包”中,从纯粹的可能性上考虑,我们有三种选择:
- 接口位于实现方所在的包中
- 接口位于调用方所在的包中
- 接口位于独立的包中
1、接口位于【调用方】所在的包中
对于类似这种情况下接口,我们将其称为 SPI, SPI的规则如下:
- 概念上更依赖调用方。
- 组织上位于调用方所在的包中。
- 实现位于独立的包中。
常见的例子是:插件模式的插件。如:
- 数据库驱动 Driver
- 日志 Log
- dubbo扩展点开发
2、接口位于【实现方】所在的包中
对于类似这种情况下的接口,我们将其称作为API,API的规则如下:
- 概念上更接近实现方。
- 组织上位于实现方所在的包中。
3、接口位于独立的包中
如果一个“接口”在一个上下文是API,在另一个上下文是SPI,那么你就可以这么组织
需要注意的事项
SPI 和 API 也不一定是接口,我这里都是指狭义的具体的接口。

Java类库中的实例
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。
---------------------
作者:在云端123
来源:CSDN
原文:https://blog.csdn.net/jyxmust/article/details/82562242
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件
边栏推荐
- NFT交易平台数字藏品系统开发技术
- Two ways of traversing binary tree: preorder, inorder and postorder
- 【软工】获取需求
- KNN classification of MATLAB (with source code) is used to realize pixel classification (set the proportion of training set by yourself) and print test accuracy
- TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
- SQLMap-hh
- 马尔可夫链(Markov Chain),隐马尔可夫模型
- IO之Buffered流案例
- 美团基于 Flink 的实时数仓平台建设新进展
- 牛客练习赛94F题解
猜你喜欢

“不敢去怀疑代码,又不得不怀疑代码”记一次网络请求超时分析

配置GPU版本的pytorch和torchvision,初学GPU版本torch踩坑
![[Software Engineering] Introduction & process and life cycle modeling](/img/92/433e2fae846406252ee0d4c47bd54b.png)
[Software Engineering] Introduction & process and life cycle modeling

Niuke challenge 53c

CF751E Phys Ed Online

【软工】 设计模块

Cloud minimalist deployment svelte3 chat room

2022过半,没有新风口

【软工】获取需求

Utilisation de la classification knn de Matlab (avec code source), réalisation de la classification des pixels (auto - réglage de l'échelle de l'ensemble de formation), précision de l'essai d'impressi
随机推荐
Bytearraystream case of IO
canvas简单的粒子效果的实现
Dirichlet prefix and study notes
Reader case of IO
The software used is PHP MySQL database
【软工】获取需求
CF736 D2
The use of cellstr function in MATLAB
[Software Engineering] Introduction & process and life cycle modeling
Arc128 C convex hull optimized suffix and?
2022 state of agile coaching Report
Kruskal reconstruction tree
NFT交易平台数字藏品系统开发技术
R language uses user-defined functions to write step activation functions for deep learning and visualize step activation functions
Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)
1.11 haas506 2.0 development tutorial driver RTC (only versions above 2.2 are supported)
Arm load storage instruction
Cookies and sessions for answers to common interview questions
electron添加SQLite數據庫
牛客挑战赛54F题解 & 李超树学习笔记