Java SPI framework
spispi
SpiLoader
SPIspi
SPI
@Spi
public interface IPrint {
void print(String str);
}
public class FilePrint implements IPrint {
@Override
public void print(String str) {
System.out.println("file print: " + str);
}
}
public class ConsolePrint implements IPrint {
@Override
public void print(String str) {
System.out.println("console print: " + str);
}
}
@Test
public void testPrint() throws NoSpiMatchException {
SpiLoader<IPrint> spiLoader = SpiLoader.load(IPrint.class);
IPrint print = spiLoader.getService("ConsolePrint");
print.print("console---->");
}
SpiLoader<IPrint> spiLoader = SpiLoader.load(IPrint.class);
action ,
@Spi
currentSelector
@SpiAdaptive
currentMethodSelector
IPrint print = spiLoader.getService("ConsolePrint");
name
spiImplClassCacheMap
ServiceLoader.load()
@SpiConf
SpiImplWrapper
SpiImplWrapper
currentSelector.select()
GroovyEngineSPIgroovy
@SuppressWarnings("unchecked")
public static <T> T compile(String code, Class<T> interfaceType, ClassLoader classLoader) throws SpiProxyCompileException {
GroovyClassLoader loader = new GroovyClassLoader(classLoader);
Class clz = loader.parseClass(code);
if (!interfaceType.isAssignableFrom(clz)) {
throw new IllegalStateException("illegal proxy type!");
}
try {
return (T) clz.newInstance();
} catch (Exception e) {
throw new SpiProxyCompileException("init spiProxy error! msg: " + e.getMessage());
}
}