Java|Java spi(service provider interface)
什么是spi,下面以mysql驱动为示例,直接贴源码。
1、jdbc在没有采用spi的情况下,是需要我们
Class.forName("com.mysql.jdbc.Driver");
2、看核心类java.sql.DriverManager.
public class DriverManager {/* Prevent the DriverManager class from being instantiated. */
private DriverManager(){}/**
* 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");
}private static void loadInitialDrivers() {
String drivers;
try {
drivers = AccessController.doPrivileged(new PrivilegedAction() {
public String run() {
return System.getProperty("jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
// 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() {
public Void run() {ServiceLoader loadedDrivers = ServiceLoader.load(Driver.class);
Iterator 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(":");
println("number of Drivers:" + driversList.length);
for (String aDriver : driversList) {
try {
println("DriverManager.Initialize: loading " + aDriver);
Class.forName(aDriver, true,
ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}
}
}
代码说明:重点看
ServiceLoader
以及
for (String aDriver : driversList) {
try {
println("DriverManager.Initialize: loading " + aDriver);
Class.forName(aDriver, true,
ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}
}
3、看下mysql-connector-java-5.1.46.jar的spi实现
文章图片
20181118215424.png 【Java|Java spi(service provider interface)】META-INF/services/java.sql.Driver的内容是:
com.mysql.jdbc.Driver
com.mysql.fabric.jdbc.FabricMySQLDriver
4、最后贴出Driver实现类的源码:
package com.mysql.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
public Driver() throws SQLException {
}static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
package com.mysql.fabric.jdbc;
import com.mysql.jdbc.ExceptionInterceptor;
import com.mysql.jdbc.NonRegisteringDriver;
import com.mysql.jdbc.Util;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;
public class FabricMySQLDriver extends NonRegisteringDriver implements Driver {
public static final String FABRIC_URL_PREFIX = "jdbc:mysql:fabric://";
public static final String FABRIC_SHARD_KEY_PROPERTY_KEY = "fabricShardKey";
public static final String FABRIC_SHARD_TABLE_PROPERTY_KEY = "fabricShardTable";
public static final String FABRIC_SERVER_GROUP_PROPERTY_KEY = "fabricServerGroup";
public static final String FABRIC_PROTOCOL_PROPERTY_KEY = "fabricProtocol";
public static final String FABRIC_USERNAME_PROPERTY_KEY = "fabricUsername";
public static final String FABRIC_PASSWORD_PROPERTY_KEY = "fabricPassword";
public static final String FABRIC_REPORT_ERRORS_PROPERTY_KEY = "fabricReportErrors";
public FabricMySQLDriver() throws SQLException {
}public Connection connect(String url, Properties info) throws SQLException {
Properties parsedProps = this.parseFabricURL(url, info);
if (parsedProps == null) {
return null;
} else {
parsedProps.setProperty("fabricProtocol", "http");
if (Util.isJdbc4()) {
try {
Constructor> jdbc4proxy = Class.forName("com.mysql.fabric.jdbc.JDBC4FabricMySQLConnectionProxy").getConstructor(Properties.class);
return (Connection)Util.handleNewInstance(jdbc4proxy, new Object[]{parsedProps}, (ExceptionInterceptor)null);
} catch (Exception var5) {
throw (SQLException)(new SQLException(var5.getMessage())).initCause(var5);
}
} else {
return new FabricMySQLConnectionProxy(parsedProps);
}
}
}public boolean acceptsURL(String url) throws SQLException {
return this.parseFabricURL(url, (Properties)null) != null;
}Properties parseFabricURL(String url, Properties defaults) throws SQLException {
return !url.startsWith("jdbc:mysql:fabric://") ? null : super.parseURL(url.replaceAll("fabric:", ""), defaults);
}public Logger getParentLogger() throws SQLException {
throw new SQLException("no logging");
}static {
try {
DriverManager.registerDriver(new FabricMySQLDriver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver", var1);
}
}
}
}
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- live|live to inspire 一个普通上班族的流水账0723
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用