建立API模块-dubbo-spi-demo-api
- api添加依赖
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.5</version>
</dependency>
</dependencies>
- 建立Service-添加spi注解
@SPI
public interface HelloService {
String sayHello();
}
- 建立实现-依赖上面的模块
public class DogHelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "hello dog";
}
}
public class HumanHelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "hello Human";
}
}
- 在resources目录下建立META-INF/dubbo目录
文件名就是接口名-内容key=value
dog=com.liu.service.impl.DogHelloServiceImpl
human=com.liu.service.impl.HumanHelloServiceImpl
- 测试
ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);
Set<String> supportedExtensions = extensionLoader.getSupportedExtensions();
supportedExtensions.forEach(s->{
System.out.println(extensionLoader.getExtension(s).sayHello());
});
hello dog
hello Human
- 添加spi默认-在接口上
@SPI("dog")
public interface HelloService {
String sayHello();
}
- 测试修改
public static void main(String[] args) {
ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);
Set<String> supportedExtensions = extensionLoader.getSupportedExtensions();
supportedExtensions.forEach(s->{
System.out.println(extensionLoader.getExtension(s).sayHello());
});
System.out.println("===============");
HelloService defaultExtension = extensionLoader.getDefaultExtension();
System.out.println(defaultExtension.sayHello());
}
}
hello dog
hello Human
===============
hello dog
getDefaultExtension可以获取默认配置的是哪个接口实现
- JDK 标准的 SPI 会一次性实例化扩展点所有实现,如果有扩展实现初始化很耗时,但如果没用上也加
载,会很浪费资源 - 如果有扩展点加载失败,则所有扩展点无法使用
Adaptive
@SPI("dog")
public interface HelloService {
String sayHello();
@Adaptive
String sayHello(URL url);
}
public class DogHelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "hello dog";
}
@Override
public String sayHello(URL url) {
return "hello dog : "+url;
}
}
public class HumanHelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "hello Human";
}
@Override
public String sayHello(URL url) {
return "hello Human "+url;
}
}
public class AdaptiveMain {
public static void main(String[] args) {
/**
* 这里注意?后面
* hello.service=HelloService
* =后面就是在配置文件中的key
*/
URL url = URL.valueOf("test://localhost/hello?hello.service=human");
ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);
HelloService helloService = extensionLoader.getAdaptiveExtension();
System.out.println(helloService.sayHello(url));
}
}
hello Human test://localhost/hello?hello.service=human
如果不写就调用@SPI注解配置的,
public class AdaptiveMain {
public static void main(String[] args) {
/**
* 这里注意?后面
* hello.service=HelloService
* =后面就是在配置文件中的key
* ?hello.service=human
*/
URL url = URL.valueOf("test://localhost/hello");
ExtensionLoader<HelloService> extensionLoader = ExtensionLoader.getExtensionLoader(HelloService.class);
HelloService helloService = extensionLoader.getAdaptiveExtension();
System.out.println(helloService.sayHello(url));
}
}
hello dog : test://localhost/hello