先翻一个来自于Baeldung的介绍:

为了更通俗易懂我就没有直译,如果有不严谨的地方请大神指教。

JavaSPI的定义

Java SPI defines four main components  

SPI四个主要概念

1. Service

服务

A well-known set of programming interfaces and classes that provide access to some specific application functionality or feature.

服务是一组接口类,用来定义某些功能或特性的接口(常常是某个jar包的功能或特性),暴露给其他程序调用。

2. Service Provider Interface

服务提供者接口

An interface or abstract class that acts as a proxy or an endpoint to the service.

If the service is one interface, then it is the same as a service provider interface.

Service and SPI together are well-known in the Java Ecosystem as API.

一个接口或抽象类,用来代理一个服务端。

如果是一个接口服务,那么这就和一个服务提供者接口一样。

服务和SPI一起便是java生态中的API了。

还是来一段自己的理解吧。。。就是定义接口,用来被别人去实现

服务提供者接口 - 如果我们有一个MyService的接口,我们把接口打进jar包,留给别人去依赖并实现。那这个接口就是服务提供者的接口。我们简单的理解成这个jar是服务接口提供者。

没错,我们就是服务提供者。我们制定规则,定义接口。接下来留个各个厂商去实现各个接口。

比如JDBC,Java开发者把链接数据库的方法写成接口。这个接口只要传进去url name passwrod 就能链接DB了。这就是一个SPI

当然很重要一步的是要由每种不同的DB厂商去实现具体怎么用这么几个简单的参数去实现链接。厂商把实现打个jar包(比如:Oracle的ojdbc-xx.jar),开发者把他加到classpath中就可以链接数据库了。

3. Service Provider

服务提供者

A specific implementation of the SPI. The Service Provider contains one or more concrete classes that implement or extends the service type.

A Service Provider is configured and identified through a provider configuration file which we put in the resource directory META-INF/services. The file name is the fully-qualified name of the SPI and his content is the fully-qualified name of the SPI implementation.

The Service Provider is installed in the form of extensions, a jar file which we place in the application classpath, the java extension classpath or user-defined classpath.

不需要翻译了,简单的理解就是接口的实现,请看上面#2中的例子。Oracle开发的链接数据库的jar就是一个服务提供者。

4. ServiceLoader

At the heart of the SPI is the ServiceLoader class. This has the role of discovering and loading implementations lazily. It uses the context classpath to locate providers implementations and put them in an internal cache.

这个类ServiceLoader是SPI的核心。使用懒加载方式发现和加载服务的实现。他通过classpath的定位服务的实现,并将他们加载到jvm中。

举个例子

创建一个项目:SpiDemo(服务接口)

定义服务接口

package com.ian.demo.spi;

public interface MyService {
void sayHello();
}

打包并install安装到本地 .m2的路径下,pom.xml:

<groupId>com.ian.demo</groupId>
<artifactId>SpiDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

另起一个新的项目:SpiDemoServicesProvider(服务提供者)

导入刚才install的jar包,pom.xml:

    <dependencies>
<dependency>
<groupId>com.ian.demo</groupId>
<artifactId>SpiDemo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

实现服务接口:

public class MyServiceImpl implements MyService {
@Override
public void sayHello() {
System.out.println("Hi, There!");
}
}

打包安装到本地m2目录:

    <groupId>com.ian.demo</groupId>
<artifactId>SpiDemoServicesProvider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

另起一个项目调用接口:接口真正的使用者

main函数,ClassLoader加载接口实现

public class Main {
public static void main(String[] args) {
ServiceLoader<MyService> myServiceImplSet = ServiceLoader.load(MyService.class);
if(myServiceImplSet.iterator().hasNext()){
myServiceImplSet.iterator().next().sayHello();
}
}
}

导入上面的SPI和SPI实现jar:

<dependencies>
<dependency>
<groupId>com.ian.demo</groupId>
<artifactId>SpiDemoServicesProvider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
  <dependency>
  <groupId>com.ian.demo</groupId>
  <artifactId>SpiDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>

运行:

Hi, There!
Process finished with exit code

总结:

是不是很累赘,为啥要写三个项目?一个不就行了?还要用ServiceLoader是不是在故弄玄虚。。

当然不是。使用场景就如我上面Oracle的例子。咱们普通开发者要给公司做个项目,需要连接Oracle数据库,好在java给我们提供了连接接口,并且Oracle提供了实现,我们只要有jre和ojdbc的jar就可以了。有了SPI后,java和Oracle'的开发者就可以分工合作了。这是java典型的解构和强封装的特性。我们日常的开发中如果有多个系统合作开发的也可以这样进行。

Java SPI详细的例子的更多相关文章

  1. Java SPI 和 API,傻傻分不清?

    最近新写了一个中间件「运行时动态日志等级开关」,其中使用Java SPI机制实现了自定义配置中心,保证良好的扩展性. 项目地址,走过路过可以点个star :)https://github.com/sa ...

  2. Wget下载终极用法和15个详细的例子

    Wget下载终极用法和15个详细的例子 备注:wget  不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...

  3. Java spi机制浅谈

    最近看到公司的一些框架和之前看到的开源的一些框架的一些服务发现和接入都采用了java的spi机制. 所以简单的总结下java spi机制的思想. 我们系统里抽象的各个模块,往往有很多不同的实现方案,比 ...

  4. Java SPI机制和使用示例

    JAVA SPI 简介 SPI 是 Java 提供的一种服务加载方式,全名为 Service Provider Interface.根据 Java 的 SPI 规范,我们可以定义一个服务接口,具体的实 ...

  5. Java SPI

    一.什么是Java SPI? SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的.在java.util.ServiceLoade ...

  6. 了解一下Java SPI的原理

    了解一下Java SPI的原理 1 为什么写这篇文章? 近期,本人在学习dubbo相关的知识,但是在dubbo官网中有提到Java的 SPI,这个名词之前未接触过,所以就去看了看,感觉还是有很多地方有 ...

  7. fasttext的基本使用 java 、python为例子

    fasttext的基本使用 java .python为例子 今天早上在地铁上看到知乎上看到有人使用fasttext进行文本分类,到公司试了下情况在GitHub上找了下,最开始是c++版本的实现,不过有 ...

  8. Dubbo 扩展点加载机制:从 Java SPI 到 Dubbo SPI

    SPI 全称为 Service Provider Interface,是一种服务发现机制.当程序运行调用接口时,会根据配置文件或默认规则信息加载对应的实现类.所以在程序中并没有直接指定使用接口的哪个实 ...

  9. Java SPI 机制实现解耦与本地化

    SPI 是 Java 提供的一种服务加载方式,全名为 Service Provider Interface,可以避免在 Java 代码中写死服务的提供者,而是通过 SPI 服务加载机制进行服务的注册和 ...

随机推荐

  1. printf函数和putchar函数

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib. ...

  2. MQC集群配置错误

    这个错误说明配置里面,MQC环境配置错了 运行war包时会读到本地的配置system-config.propertites文件

  3. kafka-clients 1.0 内部响应接口文档

    AddOffsetsToTxnResponse version:0 name type defaultValue docString throttle_time_ms INT32 0 Duration ...

  4. WKWebView 网络请求Header 丢失

    WKWebView 是苹果手机上主要的H5加载控件,它相比UIWebView 有诸多优势.在次不做比较,但是它的坑缺比较多.网上也有很多的例子但是做的比较好的真不多,我在这里推荐俩博客供大家参考.ht ...

  5. 聊一聊mycat数据库集群系列之双主双重实现

    最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据库安装.mycat部署安装.数据库之读写分离 ...

  6. IP-master

    http://www.igotaobao.cn/IP-master/

  7. magento2 定时任务

    * * * * * /usr/bin/php /www/wwwroot/m2.demo.evebit.cn/bin/magento cron:run | grep -v "Ran jobs ...

  8. WordCount of Software Engineering

    1.Github项目地址:https://github.com/BayardM/WordCount 2.PSP表格(before): PSP2.1 Personal Software Process ...

  9. Networks of Spiking Neurons: The Third Generation of Neural Network Models

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 顺便安利一下同组的大佬做的SNN教程:https://spikingflow.readthedocs.io/zh_CN/latest/Tu ...

  10. python编程中的并发------多进程multiprocessing

    任务例子:喝水.吃饭动作需要耗时1S 单任务:(耗时20s) for i in range(10): print('a正在喝水') time.sleep(1) print('a正在吃饭') time. ...