java spi与api_JAVA—API和SPI概念

目录sql

概念框架

JDBC实例ide

本身实现一个SPI.net

总结orm

概念

英文:blog

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 goalip

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.

大体含义:

服务提供接口(SPI)和应用程序接口(API)有什么不一样?

具体来讲,在JAVA类库中,是什么组成了API或者SPI?

API是你能够调用或者使用类/接口/方法等去完成某个目标的意思。

SPI是你须要继承或实现某些类/接口/方法等去完成某个目标的意思。

换句话说,API制定的类/方法能够作什么,而SPI告诉你你必须符合什么规范。

有时候SPI和API互相重叠。例如JDBC驱动类是SPI的一部分:若是仅仅是想使用JDBC驱动,你不须要实现这个类,但若是你想要实现JdBC驱动那就必须实现这个类。

关于SPI和API:你使用JDBC驱动时可使用现成的,该驱动由须要JDBC驱动开发者实现。

归纳:

API:API由开发人员调用。

SPI:SPI是框架接口规范,须要框架开发人员实现。

JDBC实例

1.Class.forName("com.mysql.jdbc.Driver");

2.Connection conn = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test", "root", "123456");

3.Statement stmt = conn.createStatement();

4.ResultSet rs = stmt.executeQuery("select * from Users");

步骤二、三、4能够看作是API的调用。而DriverManager、Conn的编写则能够看作是SPI,实现制定的接口。

本身实现一个SPI

如今咱们来实现一个如JDBC加载驱动这样模式的实例,咱们定义一个消息驱动接口,实现并调用该驱动API。

消息驱动接口:

public interface IMsg {

//发送消息,打印到控制台

void send(String msg);

}

消息驱动管理:

用来根据不一样消息驱动的实现,得到不一样驱动。

public class MsgManage {

private static final Map> map = new HashMap>();

public static void register(String protocol, Class extends IMsg> cls) {

map.put(protocol, cls);

}

public IMsg getMsgConnection(String protocol) {

try {

return map.get(protocol).newInstance();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

return null;

}

}

实现MyMsg消息驱动类:

public class MyMsg implements IMsg{

static {

MsgManage.register("my", MyMsg.class);

}

public void send(String msg) {

System.out.println("[经过MyMsg实现加载]" + msg);

}

}

实现YourMsg消息驱动类:

public class YourMsg implements IMsg{

static {

MsgManage.register("your", YourMsg.class);

}

public void send(String msg) {

System.out.println("[经过YourMsg实现加载]" + msg);

}

}

调用:

public class SpiD {

@Test

public void test() throws ClassNotFoundException {

Class.forName("com.owl.zookeeper.use.spi.MyMsg");

Class.forName("com.owl.zookeeper.use.spi.YourMsg");

IMsg my = new MsgManage().getMsgConnection("my");

IMsg your = new MsgManage().getMsgConnection("your");

my.send("你好,世界");

your.send("你好,世界");

}

}

输出:

[经过MyMsg实现加载]你好,世界

[经过YourMsg实现加载]你好,世界

总结

能够看出API主要是指调用已经实现的类去完成工做,使用者主要是程序开发人员。SPI主要是指框架扩展的规范,实现该规范能够扩展你框架的功能,使用者主要是框架开发人员。

---------------------

做者:王厚平

来源:CSDN

原文:https://blog.csdn.net/whp1473/article/details/80164254

版权声明:本文为博主原创文章,转载请附上博文连接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值