我的ServiceLoader不想加载我的新服务。我想我已经正确设置了一切。
Core.Main
public class Main {
public static void main(String[] args) {
System.out.println("Before");
Iterator<IService> services = ServiceLoader.load(IService.class).iterator();
while (services.hasNext()) {
IService service = services.next();
System.out.println(service.sayHi());
}
System.out.println("After");
}
}
Core.service.IService
package service;
public interface IService {
String sayHi();
}
DbDataBuilder.builder.DbDataBuilder
package builder;
import service.IService;
public class DbDataBuilder implements IService {
public DbDataBuilder() {
}
public String sayHi() {
return "I am Data Builder";
}
}
META-INF /服务/ service.Iservice
builder.DbDataBuilder
但是当我运行它时,控制台输出是:
Before
After
我有什么问题吗?
答案 0 :(得分:0)
当构建器在运行时位于类路径上而不是在编译时,ServiceLoader非常有用。您应该将具有编译范围依赖性的第三个模块添加到Core,并将运行时范围依赖关系添加到DbDataBuilder,将Main移动到第三个模块并享受ServiceLoader功能