API 和 SPI
簡介:
API:Application Programming Interface應(yīng)用程序接口
SPI:Service Provider Interface服務(wù)商提供接口
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是你可以直接調(diào)用或使用類/接口/方法來達成目標(biāo),它清楚地告訴你可以完成什么目標(biāo),用戶可以即插即用;
SPI是想要達成某個目標(biāo),你必須要繼承或?qū)崿F(xiàn)它,它一般只供某些特殊用途的接口開發(fā)商使用
有時候,API和SPI的界限沒有那么清楚。比如Connection接口、Driver接口
關(guān)系如圖:
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 實現(xiàn),其它的都是 Api。
參考:
https://www.cnblogs.com/happyframework/archive/2013/09/17/3325560.html
https://blog.csdn.net/whp1473/article/details/80164254
總結(jié)
- 上一篇: 夏天喝热水能减肥吗
- 下一篇: 薏米茯苓芡实粉怎么样