目录
一、SPI简介
SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制。 目前有不少框架用它来做服务的扩展发现,简单来说,它就是一种动态替换发现的机制。
使用SPI机制的优势是实现解耦,使得第三方服务模块的装配控制逻辑与调用者的业务代码分离。
二、 JDK中的SPI
Java中如果想要使用SPI功能,先提供标准服务接口,然后再提供相关接口实现和调用者。这样就可以通过SPI机制中约定好的信息进行查询相应的接口实现。
SPI遵循如下约定:
1. 当服务提供者提供了接口的一种具体实现后,在META-INF/services目录下创建一个以“接口全限定名”为命名的文件,内容为实现类的全限定名;
2. 接口实现类所在的jar包放在主程序的classpath中;
3. 主程序通过java.util.ServiceLoader动态装载实现模块,它通过扫描META-INF/services目录下的配置文件找到实现类的全限定名,把类加载到JVM;
4. SPI的实现类必须携带一个无参构造方法
以下为代码实现:
创建三个module
- java-spi-demo-api:提供接口
- java-spi-demo-impl:实现接口
- java-spi-demo-main:调用接口
1、java-spi-demo-api
创建接口
package com.lagou;
public interface HelloService {
String sayHello();
}
2、java-spi-demo-impl
①引用api
<dependencies>
<dependency>
<groupId>com.lagou</groupId>
<artifactId>java-spi-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
②创建两个实现类
package com.lagou.service;
import com.lagou.HelloService;
public class DogHelloService implements HelloService {
@Override
public String sayHello() {
return "wang wang";
}
}
package com.lagou.service;
import com.lagou.HelloService;
public class HumanHelloService implements HelloService {
@Override
public String sayHello() {
return "hello 你好";
}
}
③配置
在resources/META-INF/services目录下创建文件
com.lagou.HelloService
内容是实现接口类的全限定类名
com.lagou.service.HumanHelloService
com.lagou.service.DogHelloService
3、java-spi-demo-main
①引用上面两个demo
<dependencies>
<dependency>
<groupId>com.lagou</groupId>
<artifactId>java-spi-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.lagou</groupId>
<artifactId>java-spi-demo-impl</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
②测试
package com.lagou.test;
import com.lagou.HelloService;
import java.util.ServiceLoader;
public class JavaSpiMain {
public static void main(String[] args) {
ServiceLoader<HelloService> helloServices = ServiceLoader.load(HelloService.class);
for (HelloService helloService : helloServices) {
System.out.println(helloService.getClass().getName() + ":" + helloService.sayHello());
}
}
}