SPI和service provider framework介绍

本文深入解析了服务提供者接口(SPI)与应用程序编程接口(API)的区别,通过Java JDBC示例,介绍了SPI与API的不同应用场景及其实现方式。

我的理解:A设计了一个框架,并且实现了大部分功能,并提供了让B调用的接口即API。但是,A实现的框架中,某些部分必须依赖于B的具体场景,但B只是使用者,没必要研究怎么补全A的框架,此时,服务提供商C,提供了这部分的实现。

         即,用户B如果要正常使用A提供的API,那么必须保证A框架时完整的,就必须将A没有实现的部分用C的实现来补全。这里的A无法实现的要让C来补全的部分,通过接口形式表现出来,即SPI。



以下内容来自:http://blog.youkuaiyun.com/mosquitolxw/article/details/25290315

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 goal

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.


以下内容来自:http://www.cnblogs.com/happyframework/p/3349087.html

背景

Java 中区分 Api 和 Spi,通俗的讲:Api 和 Spi 都是相对的概念,他们的差别只在语义上,Api 直接被应用开发人员使用,Spi 被框架扩张人员使用,详细内容可以看:http://www.cnblogs.com/happyframework/p/3325560.html

Java类库中的实例

代码

复制代码
1         Class.forName("com.mysql.jdbc.Driver");
2         Connection conn = DriverManager.getConnection(
3                 "jdbc:mysql://localhost:3306/test", "root", "123456");
4         Statement stmt = conn.createStatement();
5 
6         ResultSet rs = stmt.executeQuery("select * from Users");
复制代码

说明

java.sql.Driver 是 Spi,com.mysql.jdbc.Driver 是 Spi 实现,其它的都是 Api。

如何实现这种结构?

代码

复制代码
 1 public class Program {
 2 
 3     public static void main(String[] args) throws InstantiationException,
 4             IllegalAccessException, ClassNotFoundException {
 5         Class.forName("SpiA");
 6 
 7         Api api = new Api("a");
 8         api.Send("段光伟");
 9     }
10 }
复制代码
复制代码
 1 import java.util.*;
 2 
 3 public class Api {
 4     private static HashMap<String, Class<? extends Spi>> spis = new HashMap<String, Class<? extends Spi>>();
 5     private String protocol;
 6 
 7     public Api(String protocol) {
 8         this.protocol = protocol;
 9     }
10 
11     public void Send(String msg) throws InstantiationException,
12             IllegalAccessException {
13         Spi spi = spis.get(protocol).newInstance();
14 
15         spi.send("消息发送开始");
16         spi.send(msg);
17         spi.send("消息发送结束");
18     }
19 
20     public static void Register(String protocol, Class<? extends Spi> cls) {
21         spis.put(protocol, cls);
22     }
23 }
复制代码
1 public interface Spi {
2     void send(String msg);
3 }
复制代码
 1 public class SpiA implements Spi {
 2     static {
 3         Api.Register("a", SpiA.class);
 4     }
 5 
 6     @Override
 7     public void send(String msg) {
 8         System.out.println("SpiA:" + msg);
 9     }
10 
11 }
复制代码

说明

Spi 实现的加载可以使用很多种方式,文中是最基本的方式。


以下内容来自:http://stackoverflow.com/questions/2954372/difference-between-spi-and-api

From Effective Java 2nd Edition:

A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations.

There are three essential components of a service provider framework: a service interface, which providers implement; a provider registration API, which the system uses to register implementations, giving clients access to them; and a service access API, which clients use to obtain an instance of the service. The service access API typically allows but does not require the client to specify some criteria for choosing a provider. In the absence of such a specification, the API returns an instance of a default implementation. The service access API is the “flexible static factory” that forms the basis of the service provider framework.

An optional fourth component of a service provider framework is a service provider interface, which providers implement to create instances of their service implementation. In the absence of a service provider interface, implementations are registered by class name and instantiated reflectively (Item 53). In the case of JDBC, Connection plays the part of the service interface, DriverManager.registerDriver is the provider registration API, DriverManager.getConnection is the service access API, and Driver is the service provider interface.

There are numerous variants of the service provider framework pattern. For example, the service access API can return a richer service interface than the one required of the provider, using the Adapter pattern [Gamma95, p. 139]. Here is a simple implementation with a service provider interface and a default provider:

// Service provider framework sketch

// Service interface
public interface Service {
    ... // Service-specific methods go here
}

// Service provider interface
public interface Provider {
    Service newService();
}

// Noninstantiable class for service registration and access
public class Services {
    private Services() { }  // Prevents instantiation (Item 4)

    // Maps service names to services
    private static final Map<String, Provider> providers =
        new ConcurrentHashMap<String, Provider>();
    public static final String DEFAULT_PROVIDER_NAME = "<def>";

    // Provider registration API
    public static void registerDefaultProvider(Provider p) {
        registerProvider(DEFAULT_PROVIDER_NAME, p);
    }
    public static void registerProvider(String name, Provider p){
        providers.put(name, p);
    }

    // Service access API
    public static Service newInstance() {
        return newInstance(DEFAULT_PROVIDER_NAME);
    }
    public static Service newInstance(String name) {
        Provider p = providers.get(name);
        if (p == null)
            throw new IllegalArgumentException(
                "No provider registered with name: " + name);
        return p.newService();
    }
}



### Java SPI机制概述 Java SPIService Provider Interface)是一种用于发现服务提供者实现的设计模式。它允许开发者在运行时动态加载替换组件的实现,从而增强系统的灵活性可扩展性[^1]。 #### 什么是Java SPI? Java SPI的核心思想是定义一组接口或抽象类作为标准的服务描述符,并由不同的模块或第三方来实现这些接口。通过这种方式,可以在不修改核心代码的情况下轻松切换具体实现[^2]。 --- ### Java SPI的工作原理 Java SPI的主要工作流程依赖于`java.util.ServiceLoader`类完成。以下是其实现的关键步骤: 1. **定义接口** 开发人员首先需要定义一个接口或抽象类,该接口充当服务的标准契约。 2. **创建实现类** 不同的模块可以根据需求实现上述接口,形成具体的业务逻辑。 3. **配置文件声明** 实现类的信息被记录在一个特定路径下的配置文件中,通常位于`META-INF/services/`目录下。例如,如果有一个名为`com.example.Search`的接口,则对应的配置文件应命名为`META-INF/services/com.example.Search`,其中每一行指定一个实现类的全限定名。 4. **加载并使用实现** 使用`ServiceLoader.load()`方法加载所有可用的实现类实例,并通过迭代器访问它们。 --- ### 示例代码展示 以下是一个简单的例子,演示如何利用SPI机制加载日志服务的不同实现。 #### 定义接口 ```java package com.example; public interface Logger { void info(String message); void debug(String message); } ``` #### 创建实现类 ```java // 文件:ConsoleLogger.java package com.example.impl; import com.example.Logger; public class ConsoleLogger implements Logger { @Override public void info(String message) { System.out.println("[INFO] " + message); } @Override public void debug(String message) { System.out.println("[DEBUG] " + message); } } // 文件:FileLogger.java package com.example.impl; import com.example.Logger; public class FileLogger implements Logger { @Override public void info(String message) { // 将消息写入文件... System.out.println("[FILE-INFO] " + message); } @Override public void debug(String message) { // 将调试信息写入文件... System.out.println("[FILE-DEBUG] " + message); } } ``` #### 配置文件 在项目的资源目录下创建如下结构: ``` src/main/resources/META-INF/services/ ``` 新增文件 `META-INF/services/com.example.Logger`,内容为: ``` com.example.impl.ConsoleLogger com.example.impl.FileLogger ``` #### 加载与调用 ```java package com.example.test; import com.example.Logger; import java.util.ServiceLoader; public class TestSPILoader { public static void main(String[] args) { ServiceLoader<Logger> loader = ServiceLoader.load(Logger.class); for (Logger logger : loader) { logger.info("这是一个测试信息"); logger.debug("这是调试信息"); } } } ``` 当程序执行时,会自动扫描`META-INF/services/`中的配置文件,并加载相应的实现类[^4]。 --- ### 应用场景 Java SPI广泛应用于各种开源项目技术栈中,常见的应用场景包括但不限于以下几个方面: - 日志框架SLF4J支持多种底层日志实现(如Logback、Log4j),正是基于SPI机制实现的[^3]。 - 数据库驱动管理(JDBC Driver Manager)也采用了类似的思路,在运行期间注册不同厂商提供的数据库连接驱动。 - Spring Framework Dubbo 的扩展点设计部分借鉴了SPI的思想,尽管两者有各自独特的改进之处[^2]。 --- ### 总结 Java SPI作为一种轻量级的服务发现机制,极大地简化了插件化架构的设计复杂度。然而需要注意的是,由于其完全依靠约定俗成的方式运作,因此对于错误处理以及性能优化等方面还需额外关注。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值