JAVA—API和SPI概念

JAVA—API和SPI概念mysql

 

目錄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<String, Class<? extends IMsg>> map = new HashMap<String, Class<? extends IMsg>>();

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 版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索