spi 是一种服务发现的标准,对于开发中我们通常需要编写 META-INF/services
文件夹中定义的类。
google auto 中的autoservice 可以帮助我们生成对应的配置,很方便
环境准备
- 项目结构
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ ├── spi
│ │ │ └── TranslationService.java
│ │ └── spimpl
│ │ ├── BingTranslationServiceProvider.java
│ │ └── GoogleTranslationServiceProvider.java
│ └── resources
└── test
└── java
└── com
└── dalong
└── SPITestClass.java
- 代码说明
约定:
spi中的TranslationService.java 为接口定义
BingTranslationServiceProvider.java 以及GoogleTranslationServiceProvider.java 为接口的实现
pom.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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<auto-service.version>1.0-rc6</auto-service.version>
</properties>
<groupId>com.dalong.appdemo</groupId>
<artifactId>first</artifactId>
<version>1.0-SNAPSHOT</version>
?
<dependencies>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<version>${auto-service.version}</version>
<optional>true</optional>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${auto-service.version}</version>
<optional>true</optional>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
?
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${auto-service.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
接口定义:
TranslationService.java
package com.dalong.spi;
?
import java.util.Locale;
?
public interface TranslationService {
String translate(String message, Locale from, Locale to);
}
实现:
BingTranslationServiceProvider.java 以及GoogleTranslationServiceProvider.java
package com.dalong.spimpl;
?
import com.dalong.spi.TranslationService;
import com.google.auto.service.AutoService;
?
import java.util.Locale;
@AutoService(TranslationService.class)
public class BingTranslationServiceProvider implements TranslationService {
@Override
public String translate(String message, Locale from, Locale to) {
return "bing";
}
}
?package com.dalong.spimpl;
?
import com.dalong.spi.TranslationService;
import com.google.auto.service.AutoService;
?
import java.util.Locale;
@AutoService(TranslationService.class)
public class GoogleTranslationServiceProvider implements TranslationService {
@Override
public String translate(String message, Locale from, Locale to) {
return "google";
}
}
?
打包构建
mvn clean package
效果
测试
- 代码
package com.dalong;
?
import com.dalong.spi.TranslationService;
import com.sun.prism.shader.AlphaOne_Color_AlphaTest_Loader;
import org.junit.Test;
?
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
?
import static org.junit.Assert.assertEquals;
?
public class SPITestClass {
@Test
public void myTestForSPI(){
ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
long count = StreamSupport.stream(loader.spliterator(), false).count();
?
loader.forEach(new Consumer<TranslationService>() {
@Override
public void accept(TranslationService translationService) {
Locale localeFrom = new Locale("en");
Locale localeFo = new Locale("zh");
String message = translationService.translate("app", localeFrom,localeFo);
System.out.println(message);
}
});
assertEquals(2, count);
}
}
说明
基于spi 我们可以开发比较灵活的模块系统,同时可以方便的进行模块的插拔处理
参考资料
https://github.com/google/auto/tree/master/service
https://www.jianshu.com/p/46b42f7f593c
原文地址:https://www.cnblogs.com/rongfengliang/p/11695684.html
时间: 2024-10-08 06:23:56