标签:扩展性 stat conf system 适用于 代码 out 实现 vat
假设有三个模块service, api, core
public interface MyService {
void say();
}
// MyServiceImpl1.java
public class MyServiceImpl1 implements MyService {
private static final String NAME = "MyServiceImpl1";
@Override
public void say() {
System.out.println("hello, I‘m " + NAME);
}
}
// MyServiceImpl2.java
public class MyServiceImpl2 implements MyService {
private static final String NAME = "MyServiceImpl2";
@Override
public void say() {
System.out.println("hello, I‘m " + NAME);
}
}
top.simuhunluo.impl.MyServiceImpl1
top.simuhunluo.impl.MyServiceImpl2
api中写一个类文件,通过spi调用所有的接口实现
public class Main {
public static void main(String[] args) {
ServiceLoader<MyService> services = ServiceLoader.load(MyService.class);
for (MyService service : services) {
service.say();
}
}
}
标签:扩展性 stat conf system 适用于 代码 out 实现 vat
原文地址:https://www.cnblogs.com/simuhunluo/p/13161823.html