Skip to content

Commit

Permalink
SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
deipss committed Jan 11, 2024
1 parent b4a4205 commit ab96655
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions docs/Java/SPI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
layout: default
title: SPI
parent: Java

---

# maven库

```xml

<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<version>${metainf-services.version}</version>
<scope>compile</scope>
</dependency>

```

> @MetaInfServices(InspectorPlugin.class)

# java加载SPI

Java的SPI机制就是指:针对一个接口,我们需要加载外部对该接口的实现,
只要约定好将该实现配置在classPath路径下的META-INF/services文件夹的文件,使用方就可以自动加载文件里所定义的类。

SPI中三个重要的角色:

- 接口
```java

public interface InspectorPlugin {

String identify();

boolean enable(List<String> plugins);

boolean entrance();

int watch(ModuleEventWatcher watcher,InvocationSendService service);

}
```
- 配置文件
```java
@MetaInfServices(InspectorPlugin.class)
public class DubboProviderPlugin extends BasePlugin {
public DubboProviderPlugin( InvocationSendService invocationSendService) {
super(Constant.DUBBO_PROVIDER,true,invocationSendService);
}



@Override
public void watch(ModuleEventWatcher watcher) {
new EventWatchBuilder(watcher)
.onClass("org.apache.dubbo.rpc.filter.ContextFilter").includeBootstrap()
.onBehavior("invoke")
.onWatch(new DubboProviderEventListener(entrance, protocol,invocationSendService), Event.Type.BEFORE, Event.Type.RETURN, Event.Type.THROWS);
}
}
```
- ServiceLoader反射获取

``` java
private List<InspectorPlugin> loadInspectorPluginBySPI( ClassLoader classLoader) {
ServiceLoader<InspectorPlugin> loaded = ServiceLoader.load(InspectorPlugin.class, classLoader);
Iterator<InspectorPlugin> spiIterator = loaded.iterator();
List<InspectorPlugin> target = Lists.newArrayList();
while (spiIterator.hasNext()) {
try {
target.add(spiIterator.next());
} catch (Throwable e) {
log.error("Error load spi InspectorPlugin >>> ", e);
}
}
return target;
}


```


0 comments on commit ab96655

Please sign in to comment.