SPI ,全称为 Service Provider Interface,是一种服务发现机制。它经过在ClassPath路径下的META-INF/services文件夹查找文件,自动加载文件里所定义的类。java
这一机制为不少框架扩展提供了可能,好比在Dubbo、JDBC中都使用到了SPI机制。咱们先经过一个很简单的例子来看下它是怎么用的。mysql
首先,咱们须要定义一个接口,SPIServicesql
package com.viewscenes.netsupervisor.spi;
public interface SPIService {
void execute();
}
复制代码
而后,定义两个实现类,没别的意思,只输入一句话。数据库
package com.viewscenes.netsupervisor.spi;
public class SpiImpl1 implements SPIService{
public void execute() {
System.out.println("SpiImpl1.execute()");
}
}
----------------------我是乖巧的分割线----------------------
package com.viewscenes.netsupervisor.spi;
public class SpiImpl2 implements SPIService{
public void execute() {
System.out.println("SpiImpl2.execute()");
}
}
复制代码
最后呢,要在ClassPath路径下配置添加一个文件。文件名字是接口的全限定类名,内容是实现类的全限定类名,多个实现类用换行符分隔。 文件路径以下: bash
com.viewscenes.netsupervisor.spi.SpiImpl1
com.viewscenes.netsupervisor.spi.SpiImpl2
复制代码
而后咱们就能够经过ServiceLoader.load或者Service.providers
方法拿到实现类的实例。其中,Service.providers
包位于sun.misc.Service
,而ServiceLoader.load
包位于java.util.ServiceLoader
。框架
public class Test {
public static void main(String[] args) {
Iterator<SPIService> providers = Service.providers(SPIService.class);
ServiceLoader<SPIService> load = ServiceLoader.load(SPIService.class);
while(providers.hasNext()) {
SPIService ser = providers.next();
ser.execute();
}
System.out.println("--------------------------------");
Iterator<SPIService> iterator = load.iterator();
while(iterator.hasNext()) {
SPIService ser = iterator.next();
ser.execute();
}
}
}
复制代码
两种方式的输出结果是一致的:ide
SpiImpl1.execute()
SpiImpl2.execute()
--------------------------------
SpiImpl1.execute()
SpiImpl2.execute()
复制代码
咱们看到一个位于sun.misc包
,一个位于java.util包
,sun包下的源码看不到。咱们就以ServiceLoader.load为例,经过源码看看它里面到底怎么作的。源码分析
首先,咱们先来了解下ServiceLoader,看看它的类结构。测试
public final class ServiceLoader<S> implements Iterable<S>
//配置文件的路径
private static final String PREFIX = "META-INF/services/";
//加载的服务类或接口
private final Class<S> service;
//已加载的服务类集合
private LinkedHashMap<String,S> providers = new LinkedHashMap<>();
//类加载器
private final ClassLoader loader;
//内部类,真正加载服务类
private LazyIterator lookupIterator;
}
复制代码
load方法建立了一些属性,重要的是实例化了内部类,LazyIterator。最后返回ServiceLoader的实例。ui
public final class ServiceLoader<S> implements Iterable<S>
private ServiceLoader(Class<S> svc, ClassLoader cl) {
//要加载的接口
service = Objects.requireNonNull(svc, "Service interface cannot be null");
//类加载器
loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
//访问控制器
acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null;
//先清空
providers.clear();
//实例化内部类
LazyIterator lookupIterator = new LazyIterator(service, loader);
}
}
复制代码
查找实现类和建立实现类的过程,都在LazyIterator完成。当咱们调用iterator.hasNext和iterator.next方法的时候,实际上调用的都是LazyIterator的相应方法。
public Iterator<S> iterator() {
return new Iterator<S>() {
public boolean hasNext() {
return lookupIterator.hasNext();
}
public S next() {
return lookupIterator.next();
}
.......
};
}
复制代码
因此,咱们重点关注lookupIterator.hasNext()方法,它最终会调用到hasNextService。
private class LazyIterator implements Iterator<S>{
Class<S> service;
ClassLoader loader;
Enumeration<URL> configs = null;
Iterator<String> pending = null;
String nextName = null;
private boolean hasNextService() {
//第二次调用的时候,已经解析完成了,直接返回
if (nextName != null) {
return true;
}
if (configs == null) {
//META-INF/services/ 加上接口的全限定类名,就是文件服务类的文件
//META-INF/services/com.viewscenes.netsupervisor.spi.SPIService
String fullName = PREFIX + service.getName();
//将文件路径转成URL对象
configs = loader.getResources(fullName);
}
while ((pending == null) || !pending.hasNext()) {
//解析URL文件对象,读取内容,最后返回
pending = parse(service, configs.nextElement());
}
//拿到第一个实现类的类名
nextName = pending.next();
return true;
}
}
复制代码
固然,调用next方法的时候,实际调用到的是,lookupIterator.nextService。它经过反射的方式,建立实现类的实例并返回。
private class LazyIterator implements Iterator<S>{
private S nextService() {
//全限定类名
String cn = nextName;
nextName = null;
//建立类的Class对象
Class<?> c = Class.forName(cn, false, loader);
//经过newInstance实例化
S p = service.cast(c.newInstance());
//放入集合,返回实例
providers.put(cn, p);
return p;
}
}
复制代码
看到这儿,我想已经很清楚了。获取到类的实例,咱们天然就能够对它随心所欲了!
咱们开头说,SPI机制为不少框架的扩展提供了可能,其实JDBC就应用到了这一机制。回忆一下JDBC获取数据库链接的过程。在早期版本中,须要先设置数据库驱动的链接,再经过DriverManager.getConnection获取一个Connection。
String url = "jdbc:mysql:///consult?serverTimezone=UTC";
String user = "root";
String password = "root";
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
复制代码
在较新版本中(具体哪一个版本,笔者没有验证),设置数据库驱动链接,这一步骤就再也不须要,那么它是怎么分辨是哪一种数据库的呢?答案就在SPI。
咱们把目光回到DriverManager
类,它在静态代码块里面作了一件比较重要的事。很明显,它已经经过SPI机制, 把数据库驱动链接初始化了。
public class DriverManager {
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
}
复制代码
具体过程还得看loadInitialDrivers,它在里面查找的是Driver接口的服务类,因此它的文件路径就是:META-INF/services/java.sql.Driver。
public class DriverManager {
private static void loadInitialDrivers() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
//很明显,它要加载Driver接口的服务类,Driver接口的包为:java.sql.Driver
//因此它要找的就是META-INF/services/java.sql.Driver文件
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
Iterator<Driver> driversIterator = loadedDrivers.iterator();
try{
//查到以后建立对象
while(driversIterator.hasNext()) {
driversIterator.next();
}
} catch(Throwable t) {
// Do nothing
}
return null;
}
});
}
}
复制代码
那么,这个文件哪里有呢?咱们来看MySQL的jar包,就是这个文件,文件内容为:com.mysql.cj.jdbc.Driver
。
上一步已经找到了MySQL中的com.mysql.cj.jdbc.Driver全限定类名,当调用next方法时,就会建立这个类的实例。它就完成了一件事,向DriverManager注册自身的实例。
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
static {
try {
//注册
//调用DriverManager类的注册方法
//往registeredDrivers集合中加入实例
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
public Driver() throws SQLException {
// Required for Class.forName().newInstance()
}
}
复制代码
在DriverManager.getConnection()方法就是建立链接的地方,它经过循环已注册的数据库驱动程序,调用其connect方法,获取链接并返回。
private static Connection getConnection(
String url, java.util.Properties info, Class<?> caller) throws SQLException {
//registeredDrivers中就包含com.mysql.cj.jdbc.Driver实例
for(DriverInfo aDriver : registeredDrivers) {
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
//调用connect方法建立链接
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
return (con);
}
}catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
}
} else {
println(" skipping: " + aDriver.getClass().getName());
}
}
}
复制代码
既然咱们知道JDBC是这样建立数据库链接的,咱们能不能再扩展一下呢?若是咱们本身也建立一个java.sql.Driver文件,自定义实现类MyDriver,那么,在获取链接的先后就能够动态修改一些信息。
仍是先在项目ClassPath下建立文件,文件内容为自定义驱动类com.viewscenes.netsupervisor.spi.MyDriver
咱们的MyDriver实现类,继承自MySQL中的NonRegisteringDriver,还要实现java.sql.Driver接口。这样,在调用connect方法的时候,就会调用到此类,但实际建立的过程还靠MySQL完成。
package com.viewscenes.netsupervisor.spi
public class MyDriver extends NonRegisteringDriver implements Driver{
static {
try {
java.sql.DriverManager.registerDriver(new MyDriver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
public MyDriver()throws SQLException {}
public Connection connect(String url, Properties info) throws SQLException {
System.out.println("准备建立数据库链接.url:"+url);
System.out.println("JDBC配置信息:"+info);
info.setProperty("user", "root");
Connection connection = super.connect(url, info);
System.out.println("数据库链接建立完成!"+connection.toString());
return connection;
}
}
--------------------输出结果---------------------
准备建立数据库链接.url:jdbc:mysql:///consult?serverTimezone=UTC
JDBC配置信息:{user=root, password=root}
数据库链接建立完成!com.mysql.cj.jdbc.ConnectionImpl@7cf10a6f
复制代码