API 和 SPI

简介:
API:Application Programming Interface应用程序接口
SPI:Service Provider Interface服务商提供接口
JDK中有描述,

  • 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

翻译过来就是:
API是你可以直接调用或使用类/接口/方法来达成目标,它清楚地告诉你可以完成什么目标,用户可以即插即用;
SPI是想要达成某个目标,你必须要继承或实现它,它一般只供某些特殊用途的接口开发商使用
有时候,API和SPI的界限没有那么清楚。比如Connection接口、Driver接口

关系如图:
API&SPI

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。

参考:
https://www.cnblogs.com/happyframework/archive/2013/09/17/3325560.html
https://blog.csdn.net/whp1473/article/details/80164254

猜你喜欢

转载自blog.csdn.net/zhaohong_bo/article/details/89156237
SPI