代码拉取完成,页面将自动刷新
如:com.demo.spi.a.AService
在目录/src/main/resources/META-INF/services(也就是jar包META-INF/services/)下以接口名称为文件名创建接口声明文件。 文件内容为接口的实现类,一行一个。 例如(demo-spi-a/src/main/resources/META-INF/services/com.demo.spi.a.AService):
com.demo.spi.a.AServiceImpl1
com.demo.spi.a.AServiceImpl2
接口声明文件不需要和接口的声明和实现类在同一个jar包中。
使用ServiceLoader获取接口的实例。如:
ServiceLoader<T> aServiceServiceLoader = ServiceLoader.load(clazz);
Iterator<T> aServiceIterator = aServiceServiceLoader.iterator();
while (aServiceIterator.hasNext()) {
T next = aServiceIterator.next();
if (next instanceof AService) {
AService aservice = (AService) next;
System.out.println(aservice.greeting("a"));
} else if (next instanceof BService) {
BService bService = (BService)next;
System.out.println(bService.greeting("b"));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。