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类库中的实例

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

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

如何实现这种结构?

1public class Program { 2    public static void main(String[] args) throws InstantiationException, 3        IllegalAccessException, ClassNotFoundException { 4        Class.forName("SpiA"); 5        Api api = new Api("a"); 6        api.Send("ABC"); 7    } 8} 9 10import java.util.*; 11 12public class Api { 13    private static HashMap<String, Class<? extends Spi>> spis =  14        new HashMap<String, Class<? extends Spi>>(); 15    private String protocol; 16 17    public Api(String protocol) { 18        this.protocol = protocol; 19    } 20 21    public void Send(String msg) throws InstantiationException, 22        IllegalAccessException { 23        Spi spi = spis.get(protocol).newInstance(); 24        spi.send("消息发送开始"); 25        spi.send(msg); 26        spi.send("消息发送结束"); 27    } 28 29    public static void Register(String protocol, Class<? extends Spi> cls) { 30        spis.put(protocol, cls); 31    } 32} 33 34 35 36public interface Spi { 37    void send(String msg); 38} 39 40public class SpiA implements Spi { 41    static { 42        Api.Register("a", SpiA.class); 43    } 44 45    @Override 46    public void send(String msg) { 47        System.out.println("SpiA:" + msg); 48    } 49}

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

SPI和API的区别 - HelloWorld