SPI机制(Service Provider Interface)其实源自服务提供者框架(Service Provider Framework,参考【EffectiveJava】page6),是一种将服务接口与服务实现分离以达到解耦、大大提升了程序可扩展性的机制。引入服务提供者就是引入了spi接口的实现者,通过本地的注册发现获取到具体的实现类,轻松可插拔
典型实例:jdbc的设计
通常各大厂商(如Mysql、Oracle)会根据一个统一的规范(java.sql.Driver)开发各自的驱动实现逻辑。客户端使用jdbc时不需要去改变代码,直接引入不同的spi接口服务即可。
Mysql的则是com.mysql.jdbc.Drive,Oracle则是oracle.jdbc.driver.OracleDriver。
伪代码如下:
//注:从jdbc4.0之后无需这个操作,spi机制会自动找到相关的驱动实现
//Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库。有可能注册了多个Driver,这里通过遍历成功连接后返回。
con = DriverManager.getConnection(mysqlUrl,user,password);
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
1. java.sql.DriverManager静态块初始执行,其中使用spi机制加载jdbc具体实现
//java.sql.DriverManager.java
//当调用DriverManager.getConnection(..)时,static会在getConnection(..)执行之前被触发执行
/**
* Load the initial JDBC drivers by checking the System property
* jdbc.properties and then use the {@code ServiceLoader} mechanism
*/
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
2. loadInitialDrivers()中完成了引入的数据库驱动的查找以及载入,本示例只引入了oracle厂商的mysql,我们具体看看。
//java.util.serviceLoader.java
private static void loadInitialDrivers() {
String drivers;
try {
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
//使用系统变量方式加载
return System.getProperty("jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
//如果spi 存在将使用spi方式完成提供的Driver的加载
// If the driver is packaged as a Service Provider, load it.
// Get all the drivers through the classloader
// exposed as a java.sql.Driver.class service.
// ServiceLoader.load() replaces the sun.misc.Providers()
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
//查找具体的provider,就是在META-INF/services/***.Driver文件中查找具体的实现。
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
Iterator<Driver> driversIterator = loadedDrivers.iterator();
/* Load these drivers, so that they can be instantiated.
* It may be the case that the driver class may not be there
* i.e. there may be a packaged driver with the service class
* as implementation of java.sql.Driver but the actual class
* may be missing. In that case a java.util.ServiceConfigurationError
* will be thrown at runtime by the VM trying to locate
* and load the service.
*
* Adding a try catch block to catch those runtime errors
* if driver not available in classpath but it's
* packaged as service and that service is there in classpath.
*/
//查找具体的实现类的全限定名称
try{
while(driversIterator.hasNext()) {
driversIterator.next();//加载并初始化实现类
}
} catch(Throwable t) {
// Do nothing
}
return null;
}
});
println("DriverManager.initialize: jdbc.drivers = " + drivers);
if (drivers == null || drivers.equals("")) {
return;
}
String[] driversList = drivers.split(":");
}
}
3. java.util.ServiceLoader 加载spi实现类.
上一步的核心代码如下,我们接着分析:
//java.util.serviceLoader.java
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
Iterator<Driver> driversIterator = loadedDrivers.iterator();
try{
//查找具体的实现类的全限定名称
while(driversIterator.hasNext()) {
//加载并初始化实现
driversIterator.next();
}
} catch(Throwable t) {
// Do nothing
}
主要是通过ServiceLoader来完成的,我们按照执行顺序来看看ServiceLoader实现:
//初始化一个ServiceLoader,load参数分别是需要加载的接口class对象,当前类加载器
public static <S> ServiceLoader<S> load(Class<S> service) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return ServiceLoader.load(service, cl);
}
public static <S> ServiceLoader<S> load(Class<S> service,
ClassLoader loader)
{
return new ServiceLoader<>(service, loader);
}
遍历所有存在的service实现
public boolean hasNext() {
if (acc == null) {
return hasNextService();
} else {
PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
public Boolean run() { return hasNextService(); }
};
return AccessController.doPrivileged(action, acc);
}
}
//写死的一个目录
private static final String PREFIX = "META-INF/services/";
private boolean hasNextService() {
if (nextName != null) {
return true;
}
if (configs == null) {
try {
String fullName = PREFIX + service.getName();
//通过相对路径读取classpath中META-INF目录的文件,也就是读取服务提供者的实现类全限定名
if (loader == null)
configs = ClassLoader.getSystemResources(fullName);
else
configs = loader.getResources(fullName);
} catch (IOException x) {
fail(service, "Error locating configuration files", x);
}
}
//判断是否读取到实现类全限定名,比如mysql的“com.mysql.jdbc.Driver
”
while ((pending == null) || !pending.hasNext()) {
if (!configs.hasMoreElements()) {
return false;
}
pending = parse(service, configs.nextElement());
}
nextName = pending.next();//nextName保存,后续初始化实现类使用
return true;//查到了 返回true,接着调用next()
}
public S next() {
if (acc == null) {//用来判断serviceLoader对象是否完成初始化
return nextService();
} else {
PrivilegedAction<S> action = new PrivilegedAction<S>() {
public S run() { return nextService(); }
};
return AccessController.doPrivileged(action, acc);
}
}
private S nextService() {
if (!hasNextService())
throw new NoSuchElementException();
String cn = nextName;//上一步找到的服务实现者全限定名
nextName = null;
Class<?> c = null;
try {
//加载字节码返回class对象.但并不去初始化(换句话就是说不去执行这个类中的static块与static变量初始化)
//
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 {
//初始化这个实现类.将会通过static块的方式触发实现类注册到DriverManager(其中组合了一个CopyOnWriteArrayList的registeredDrivers成员变量)中
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
}
上一步中,Sp = service.cast(c.newInstance())
将会导致具体实现者的初始化,比如mysqlJDBC,会触发如下代码:
//com.mysql.jdbc.Driver.java
private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();
static {
try {
//并发安全的想一个copyOnWriteList中方
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
4.最终Driver全部注册并初始化完毕,开始执行DriverManager.getConnection(url, “root”, “root”)方法并返回。
转自:https://lemon.blog.csdn.net/article/details/79189475
public static void main(String[] args)
{
Enumeration<Driver> drivers = DriverManager.getDrivers();
Driver driver;
while (drivers.hasMoreElements())
{
driver = drivers.nextElement();
System.out.println(driver.getClass() + "------" + driver.getClass().getClassLoader());
}
System.out.println(DriverManager.class.getClassLoader());
}
输出结果如下:
class com.mysql.jdbc.Driver------sun.misc.Launcher A p p C l a s s L o a d e r @ 2 a 139 a 55 c l a s s c o m . m y s q l . f a b r i c . j d b c . F a b r i c M y S Q L D r i v e r − − − − − − s u n . m i s c . L a u n c h e r AppClassLoader@2a139a55 class com.mysql.fabric.jdbc.FabricMySQLDriver------sun.misc.Launcher AppClassLoader@2a139a55classcom.mysql.fabric.jdbc.FabricMySQLDriver−−−−−−sun.misc.LauncherAppClassLoader@2a139a55
null
我们发现DriverManager是null,即通过Bootstrap ClassLoader加载进来的,而com.mysql.jdbc.Driver是通过Application ClassLoader加载进来的。
由于双亲委派模型,父加载器是拿不到通过子加载器加载的类的。这里就引出了一个问题,为什么通过Bootstrap ClassLoader加载进来的DriverManager,可以拿到Application ClassLoader加载进来的com.mysql.jdbc.Driver?
这个现象,就是破坏了双亲委派模型。
因为DriverManager是通过Bootstrap ClassLoader加载进来的,而com.mysql.jdbc.Driver是通过classpath的JAR包加载进来的。要想通过DriverManager,必须破坏双亲委派模型才能拿到。
通过Thread.currentThread().setContextClassLoader(),将Application ClassLoader设置为线程上下文加载器。在DriverManager类里通过Thread.currentThread().getContextClassLoader()拿到Application ClassLoader进行加载。
SPI的这种加载方式,就只需要定义好接口,引入符合规范的jar包,就可以获取到实现该接口的类了。
有点类似于IOC,上层只需要指定一个配置文件路径,在初始化的时候去扫描所有符合的配置文件路径,并解析其中的内容,再去加载对应的类,就可以拿到所有该接口的实现了。
转自:https://www.jianshu.com/p/bfa4b171096a