Appearance
Java SPI-Service Provider Interface
Java SPI(Service Provider Interface)是Java提供的一套服务发现机制,它允许在运行时动态地加载实现特定接口的类,从而实现解耦和灵活性。也就是说服务接口与实现分离,类似的机制可以在以下案例发现:
- 数据库驱动加载: JDBC使用SPI机制加载不同的数据库驱动,使得Java程序可以连接各种数据库。
- 日志框架: slf4j使用SPI机制绑定具体的日志实现,如Logback、Log4j等。
创建父项目
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lee</groupId>
<artifactId>java-spi</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>num-interface</module>
<module>num-impl1</module>
<module>num-impl2</module>
<module>client</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>其中设置了四个模块:
- num-interface:用于定于服务接口以及获取服务实现类;
- num-impl1:用于实现服务接口;
- num-impl2:另一个服务实现类;
- client:客户端,表示使用服务的程序;
2. 服务接口模块
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-spi</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>num-interface</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>其中定义了一个服务接口:
java
public interface NumInterface {
int add(int num1, int num2);
}以及发现服务的工厂类:
java
public class NumFactory {
public static NumInterface create(){
ServiceLoader<NumInterface> serviceLoader = ServiceLoader.load(NumInterface.class);
Iterator<NumInterface> iterator = serviceLoader.iterator();
while (iterator.hasNext()) {
NumInterface service = iterator.next();
return service;
}
return null;
}
}默认使用第一个服务实现。
3. 第一个服务实现
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-spi</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>num-impl1</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.lee</groupId>
<artifactId>num-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>然后实现服务接口:
java
public class NumImpl1 implements NumInterface {
@Override
public int add(int num1, int num2) {
System.out.println("NumImpl1 实现·······");
return num1 + num2;
}
}并且在resources下创建META-INF/services目录,在其中创建名为接口全限定名的文件,其中内容是接口实现类的全限定名:
txt
com.third.NumImpl1
4. 第二个服务实现
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-spi</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>num-impl2</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.lee</groupId>
<artifactId>num-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>然后实现接口:
java
public class NumImpl2 implements NumInterface {
@Override
public int add(int num1, int num2) {
System.out.println("-----NumImpl2 实现-----");
return num1 + num2;
}
}并且在resources下创建META-INF/services目录,在其中创建名为接口全限定名的文件,其中内容是接口实现类的全限定名:
txt
org.kk.NumImpl2
5. 客户端
在客户端引入接口模块以及具体的实现模块:
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-spi</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.lee</groupId>
<artifactId>num-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.lee</groupId>
<artifactId>num-impl2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.lee.Demo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>通过工厂类获取服务并使用:
java
public class Demo {
public static void main(String[] args) {
NumInterface numInterface = NumFactory.create();
int add = numInterface.add(1, 2);
System.out.println(add);
}
}这样就实现了接口和实现分离,我们可以方便地替换实现,并通过服务接口统一使用而不用关心具体实现。
6. 限制
- SPI默认使用无参构造函数来实例化实现类。
- ServiceLoader.load()方法会遍历META-INF/services目录下的所有配置文件,并尝试实例化其中列出的所有实现类。即使某些实现类在当前场景下并不需要使用,或者实例化过程非常耗时,它们仍然会被加载和实例化,这会导致不必要的资源浪费,尤其是在实现类数量较多或者某些实现类的初始化成本较高的情况下。