了解ServiceLoader,须要先了解 SPI(Service Provider Interface)java
SPI的简单来讲就是在程序设计时将一个功能服务的接口与实现分离,在程序运行时经过JVM机制自动找到服务接口的实现类并建立,以达到解耦的目的,提升程序的可拓展性; 好比JDBCgit
ServiceLoader就是 Java平台提供的一个简单的 Service Provder Framework。使用ServiceLoader有简单的如下几个步骤github
这里以一个简单虚拟支付场景为例。 有一个业务模块目前须要使用支付服务,因此咱们首先建立了一个PaymenService抽象接口表示 支付服务,接口类中有一个抽象方法**pay(String productName,double price)**表示支付某个商品 ###建立服务实现模块缓存
package com.knight.serviceimpl;
import com.knight.PaymentService;
public class PaymentServiceImpl implements PaymentService {
@Override
public void pay(String productName, double price) {
System.out.println("支付模块:购买产品 "+productName +",价格"+price);
}
}
复制代码
在IDEA中的结构以下 app
业务模块中直接经过ServiceLoader类及PaymentService接口获取服务实例并实现业务逻辑(业务模块通常是不包含服务的实现模块的)ide
package com.knight.business;
import com.knight.PaymentService;
import java.util.Iterator;
import java.util.ServiceLoader;
public class BusinessModule {
public static void run(){
Iterator<PaymentService> serviceIterator = ServiceLoader.load(PaymentService.class).iterator();
if (serviceIterator.hasNext()){
PaymentService paymentService = serviceIterator.next();
paymentService.pay("Q币充值",100.00);
}else {
System.out.println("未找到支付模块");
}
}
}
复制代码
以上的核心代码是 经过 ServiceLoader的load方法,传入PaymentService接口类,会返回一个ServiceLoader的实例对象,经过该对象的**iterator()**方法会返回一个 Iterator 的迭代器,能够经过这个迭代器获得全部PaymentService的实现对象。gradle
最后 咱们再建立一个app模块运行业务代码逻辑,app模块包含service、service-impl、business、3个模块。ui
以上全部代码已上传 gitthis
ServiceLoader内部细节 1.首先经过静态方法load获取对应服务接口的ServiceLoader实例; 2.ServiceLoader类继承了Iterabale接口,内部实现了一个服务实例懒加载的迭代器;迭代器内部经过classLoader读 取对应META-INF/service/文件夹下的服务配置文件获取到全部的实现类类名称,当经过iterator()方法获取迭代器后,就能够依次实例化service的实现并将实现对象加入到缓存中。 3.解析配置文件的过程就是按行读取,每一行的文本都是一个服务实现类的全限定名,获取到类名就能够经过反射实例化对象了google
public final class ServiceLoader<S> implements Iterable<S> {
//Service配置文件的资源路径
private static final String PREFIX = "META-INF/services/";
// The class or interface representing the service being loaded
private final Class<S> service;
// 负责service配置资源加载,实例化service
private final ClassLoader loader;
// 服务实例的缓存,已被迭代被建立过的会加到这个cache中
private LinkedHashMap<String,S> providers = new LinkedHashMap<>();
private ServiceLoader(Class<S> svc, ClassLoader cl) {
service = Objects.requireNonNull(svc, "Service interface cannot be null");
loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
reload();
}
//重载load, 删除缓存并从新实例化iterator
public void reload() {
providers.clear();
lookupIterator = new LazyIterator(service, loader);
}
//懒加载的service迭代器
private class LazyIterator implements Iterator<S> {
Class<S> service;
ClassLoader loader;
Enumeration<URL> configs = null;
Iterator<String> pending = null;
String nextName = null;
private LazyIterator(Class<S> service, ClassLoader loader) {
this.service = service;
this.loader = loader;
}
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 ((pending == null) || !pending.hasNext()) {
if (!configs.hasMoreElements()) {
return false;
}
pending = parse(service, configs.nextElement());
}
//获取配置文件中的下一个元素
nextName = pending.next();
return true;
}
private S nextService() {
if (!hasNextService())
throw new NoSuchElementException();
String cn = nextName;
nextName = null;
Class<?> c = null;
try {
c = Class.forName(cn, false, loader);
} catch (ClassNotFoundException x) {
fail(service,
"Provider " + cn + " not found");
}
if (!service.isAssignableFrom(c)) {
fail(service,
"Provider " + cn + " not a subtype");
}
try {
S p = service.cast(c.newInstance());
providers.put(cn, p);
return p;
} catch (Throwable x) {
fail(service,
"Provider " + cn + " could not be instantiated",
x);
}
throw new Error(); // This cannot happen
}
public boolean hasNext() {
return hasNextService();
}
public S next() {
return nextService();
}
}
//按行读取文件,每一行都是服务实现类的接口名
private Iterator<String> parse(Class<?> service, URL u)throws ServiceConfigurationError {
InputStream in = null;
BufferedReader r = null;
ArrayList<String> names = new ArrayList<>();
try {
in = u.openStream();
r = new BufferedReader(new InputStreamReader(in, "utf-8"));
int lc = 1;
while ((lc = parseLine(service, u, r, lc, names)) >= 0);
} catch (IOException x) {
fail(service, "Error reading configuration file", x);
} finally {
try {
if (r != null) r.close();
if (in != null) in.close();
} catch (IOException y) {
fail(service, "Error closing configuration file", y);
}
}
return names.iterator();
}
}
复制代码
以上 当注册服务实现时若是须要手动建立文件并写入服务实现类名称 不免有些繁琐,咱们可使用谷歌提供的 AutoService 库简化这一过程
dependencies {
compileOnly 'com.google.auto.service:auto-service:1.0-rc2'
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc2'
}
复制代码
package com.knight.serviceimpl;
import com.google.auto.service.AutoService;
import com.knight.PaymentService;
@AutoService(PaymentService.class)
public class PaymentServiceImpl implements PaymentService {
@Override
public void pay(String productName, double price) {
System.out.println("支付模块:购买产品 "+productName +",价格"+price);
}
}
复制代码
3.项目编译触发auto-service注解处理过程后自动生成了配置文件
该实例源码已上传 git仓库