Java SPI详细的例子
先翻一个来自于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详细的例子的更多相关文章
- Java SPI 和 API,傻傻分不清?
最近新写了一个中间件「运行时动态日志等级开关」,其中使用Java SPI机制实现了自定义配置中心,保证良好的扩展性. 项目地址,走过路过可以点个star :)https://github.com/sa ...
- Wget下载终极用法和15个详细的例子
Wget下载终极用法和15个详细的例子 备注:wget 不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...
- Java spi机制浅谈
最近看到公司的一些框架和之前看到的开源的一些框架的一些服务发现和接入都采用了java的spi机制. 所以简单的总结下java spi机制的思想. 我们系统里抽象的各个模块,往往有很多不同的实现方案,比 ...
- Java SPI机制和使用示例
JAVA SPI 简介 SPI 是 Java 提供的一种服务加载方式,全名为 Service Provider Interface.根据 Java 的 SPI 规范,我们可以定义一个服务接口,具体的实 ...
- Java SPI
一.什么是Java SPI? SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的.在java.util.ServiceLoade ...
- 了解一下Java SPI的原理
了解一下Java SPI的原理 1 为什么写这篇文章? 近期,本人在学习dubbo相关的知识,但是在dubbo官网中有提到Java的 SPI,这个名词之前未接触过,所以就去看了看,感觉还是有很多地方有 ...
- fasttext的基本使用 java 、python为例子
fasttext的基本使用 java .python为例子 今天早上在地铁上看到知乎上看到有人使用fasttext进行文本分类,到公司试了下情况在GitHub上找了下,最开始是c++版本的实现,不过有 ...
- Dubbo 扩展点加载机制:从 Java SPI 到 Dubbo SPI
SPI 全称为 Service Provider Interface,是一种服务发现机制.当程序运行调用接口时,会根据配置文件或默认规则信息加载对应的实现类.所以在程序中并没有直接指定使用接口的哪个实 ...
- Java SPI 机制实现解耦与本地化
SPI 是 Java 提供的一种服务加载方式,全名为 Service Provider Interface,可以避免在 Java 代码中写死服务的提供者,而是通过 SPI 服务加载机制进行服务的注册和 ...
随机推荐
- Vue 给页面加水印指令(directive)
页面需要水印 import Vue from 'vue' /** * watermark 指令 * 解决: 给页面生成水印 * 基本原理:给选择器添加背景图片 * 用法:v-watermark=&qu ...
- 《闲扯Redis十》Redis 跳跃表的结构实现
一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...
- 【建议收藏】swoft的最佳实践
这是一篇使用 swoft 两个月后的总结文章!,后续会陆续更新的 这是 web-api 开发的总结,如果使用 websocket 等服务的可能不适用,本章节会对一些规范.习惯,或者优化进行一些说明 一 ...
- C#,js和sql实用技巧选2
1.为什么"foo".Equals()不是好做法?因为当正确的写法是"foo".Equals(obj.value),却写成了"foo".Eq ...
- 『JWT』,你必须了解的认证登录方案
我是风筝,公众号「古时的风筝」,一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农! 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在 ...
- Wireshark中的Checksum: 0x90c5 [validation disabled]问题
Wireshark中的Checksum: 0x90c5 [validation disabled]问题 废话不多说先上问题图: 这是我在做关于DNS协议PPT的时候出现的协议树第五项展开结果,可以发现 ...
- CVE-2020-15778 OpenSSH命令注入漏洞复现
一.漏洞概要 OpenSSH 8.3p1及之前版本中的scp允许在scp.c远程功能中注入命令,攻击者可利用该漏洞执行任意命令.目前绝大多数linux系统受影响. 参考链接:https://githu ...
- CentOS下删除物理磁盘,删除LVM
1.删除 dmsetup remove LV_name 2.vgreduce VG_name --removemissing 3.vgremove VG_name 4.pvremove disk
- src rpm 下载地址
drbd: http://mirror.rackspace.com/elrepo/elrepo/el7/SRPMS/ rabbitmq: https://dl.bintray.com/rabbitmq ...
- c++线性表和数组的区别
在传统C语言程序中,描述顺序表的存储表示有两种方式:静态方式.动态方式 顺序表的静态存储表示: #define maxSize 100 typedefintT; typedefstruct{ T da ...