记录一次ServiceLoader.load() 获取不到实例的问题
问题描述:在封装web-module时,使用了Google auto-service库,结合java spi机制,进行解耦。
由于web-module无法引用业务module-T,但需要调用T中的一个方法,故下沉一个业务接口至common层,module T实现接口并打上AutoService注解。
在web-module(jsbridge)命令模式的实现类中通过ServiceLoader.load(T.class)调用module-T 中TImpl.do()方法时,无法获取到TImpl实例。
1、刚开始以为是注解的使用方式有问题,由于使用java 的SPI,跟踪源码ServiceLoader.LazyIterator
private static final String PREFIX = "META-INF/services/";
//......
private boolean hasNextService() {
if (nextName != null) {
return true;
}
if (configs == null) {
try {
String fullName = PREFIX + service.getName();
if (loader == null)
configs = ClassLoader.getSystemResources(fullName);
else
configs = loader.getResources(fullName);
} catch (IOException x) {
fail(service, "Error locating configuration files", x);
}
}
while