SPI和API之间的区别?

SPI(服务提供者接口)和API(应用程序编程接口)在软件开发中有不同作用。API是客户端访问服务的手段,主要供用户直接使用;SPI则允许外部扩展或改变软件行为。在Java中,API提供服务的接口,而SPI用于服务提供者实现特定功能。例如,JNDI是API,提供查找上下文的接口,而其内部使用SPI进行实现。
摘要由CSDN通过智能技术生成

本文翻译自:Difference between SPI and API?

What is the difference between Service Provider Interface (SPI) and Application Programming Interface (API) ? 服务提供者接口(SPI)应用程序编程接口(API)之间有什么区别?

More specifically, for Java libraries, what makes them an API and/or SPI? 更具体地说,对于Java库,是什么使它们成为API和/或SPI?


#1楼

参考:https://stackoom.com/question/COZA/SPI和API之间的区别


#2楼

The difference between API and SPI comes when an API additionally provides some concrete implementations. 当API另外提供一些具体实现时,API和SPI之间的区别就出现了。 In that case, the service provider has to implement a few APIs (called SPI) 在这种情况下,服务提供商必须实现一些API(称为SPI)

An example is JNDI: 一个例子是JNDI:

JNDI provides interfaces & some classes for context lookup. JNDI为上下文查找提供接口和一些类。 The default way to lookup a context is provided in IntialContext. IntialContext中提供了查找上下文的默认方法。 This class internally will use SPI interfaces (using NamingManager) for provider specific implementations. 此类内部将使用SPI接口(使用NamingManager)进行提供程序特定的实现。

See the JNDI Architecture below for better understanding. 有关更好的理解,请参阅下面的JNDI体系结构。

在此输入图像描述


#3楼

API stands for Application Programming Interface, where API is a means for accessing a service / function provided by some kind of software or a platform. API代表应用程序编程接口,其中API是用于访问由某种软件或平台提供的服务/功能的手段。

SPI stands for Service Provider Interface, where SPI is way to inject, extend or alter the behavior for software or a platform. SPI代表服务提供者接口,SPI是注入,扩展或改变软件或平台行为的方式。

API is normally target for clients to access a service and it has the following properties: API通常是客户端访问服务的目标,它具有以下属性:

-->API is a programmatic way of accessing a service to achieve a certain behavior or output - > API是一种以编程方式访问服务以实现某种行为或输出

-->From API evolution point of view, addition is no problem at all for clients - >从API发展的角度来看,添加对客户来说完全没问题

-->But API's once utilized by clients it can not (and should not) be altered / deleted unless there are an appropriate communications, since its a complete degradation of the client expectation - >但是API曾经被客户使用过,除非有适当的通信,否则它不能(也不应该)被更改/删除,因为它完全降低了客户的期望

SPI on the other part are targeted for providers and has the following properties: 另一部分的SPI针对提供商,具有以下属性:

-->SPI is a way to extend / alter the behavior of a software or a platform (programmable vs. programmatic) - > SPI是一种扩展/改变软件或平台行为的方法(可编程与程序化)

-->SPI evolution is different from API evolution, in SPI removal is not an issue - > SPI演化不同于API演进,在SPI移除中不是问题

-->Addition of SPI interfaces will cause problems and may break existing implementations - >添加SPI接口会导致问题并可能破坏现有的实现

For more explanation click here : Service Provider Interface 有关更多说明,请单击此处: 服务提供商接口


#4楼

I suppose an SPI slots into a larger system by implementing certain features of an API, and then registering itself as being available via service lookup mechanisms. 我想通过实现API的某些功能将SPI插入更大的系统,然后通过服务查找机制将其注册为可用。 An API is used by the end-user application code directly, but may integrate SPI components. 最终用户应用程序代码直接使用API​​,但可以集成SPI组件。 It's the difference between encapsulation and direct usage. 这是封装和直接使用之间的区别。


#5楼

In the Java world, different technologies are meant to be modular and "pluggable" into an application server. 在Java世界中,不同的技术意味着模块化并可“插入”应用程序服务器。 There is then a difference between 然后有区别

  • the application server 应用服务器
    • [SPI] [SPI]
  • the pluggable technology 可插拔技术
    • [API] [API]
  • the end user application 最终用户应用程序

Two examples of such technologies are JTA (the transaction manager) and JCA (adapter for JMS or database). 这些技术的两个例子是JTA(事务管理器)和JCA(JMS或数据库的适配器)。 But there are others. 但还有其他人。

Implementer of such a pluggable technology must then implement the SPI to be pluggable in the app. 然后,这种可插拔技术的实现者必须将SPI实现为可插入应用程序。 server and provide an API to be used by the end-user application. 服务器并提供最终用户应用程序使用的API。 An example from JCA is the ManagedConnection interface which is part of the SPI, and the Connection that is part of the end-user API. JCA的一个示例是ManagedConnection接口,它是SPI的一部分, Connection是最终用户API的一部分。


#6楼

  • The API is the description of classes/interfaces/methods/... that you call and use to achieve a goal, and API是您调用并用于实现目标的类/接口/方法/ ...的描述,以及
  • the SPI is the description of classes/interfaces/methods/... that you extend and implement to achieve a goal. SPI是您扩展和实现以实现目标的类/接口/方法/ ...的描述。

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. 换句话说,API告诉您特定的类/方法为您做了什么,SPI告诉您必须做什么来符合。

Usually API and SPI are separate. 通常API和SPI是分开的。 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. 例如,在JDBC中Driver是SPI的一部分:如果您只是想使用JDBC,则不需要直接使用它,但实现JDBC驱动程序的每个人都必须实现该类。

Sometimes they overlap, however. 然而,有时它们重叠。 The Connection interface 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. Connection接口SPI和API:当您使用JDBC驱动程序,它需要通过JDBC驱动程序的开发者来实现您可以使用它定期。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值