quick-spi

Java SPI framework

Stars
12

SPI

spispi

SpiLoader

  1. loadspi
  2. selector
  3. spi or

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 ,

  • new
    • @Spi currentSelector
    • @SpiAdaptive currentMethodSelector

IPrint print = spiLoader.getService("ConsolePrint");

name

  • spiImplClassCacheMap
    • jdk 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());
   }
}

Related Projects