内容简介:JDK SPI源码详解
SPI是Service Provider Interface的缩写,可以使用它扩展框架和更换的组件。 JDK提供了java.util.ServiceLoader工具类,在使用某个服务接口时,它可以帮助我们查找该服务接口的实现类,加载和初始化,前提条件是基于它的约定 。
大多数开发人员可能不熟悉,却经常使用它。举个例子,获取 MySQL 数据库连接,代码如下:
public class MySQLConnect {
private final static String url ="jdbc:mysql://localhost:3306/test";
private final static String username = "root";
private final static String password = "root";
public static Connection getConnection() throws SQLException {
// Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url,username,password);
}
public static void main(String[] args) throws SQLException {
System.out.println(getConnection());
}
}
上述代码是可以运行成功的。接下来我们就分析下DriverManager.getConnection(url,username,password)的过程,探究其如何获取到数据库连接Connection?
1、首先分析loadInitialDrivers方法,其源码如下:
private static void loadInitialDrivers() {
/**
* 获取ServiceLoader实例,loadedDrivers =
* new ServiceLoader(Driver.class,Thread.currentThread().getContextClassLoader())
*/
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
/**
* 获取serviceLoader实例的迭代器,driversIterator = new LazyIterator(service, loader)
*/
Iterator<Driver> driversIterator = loadedDrivers.iterator();
/**
* 应用程序加载器(Thread.currentThread().getContextClassLoader())
* 加载classpath下META-INF/services/java.sql.Driver文件,
* 解析java.sql.Driver文件的内容将其存储在LazyIterator.pending实例变量中。
*/
while(driversIterator.hasNext()) {
/**
* 通过反射实例化java.sql.Driver文件中的类(com.mysql.jdbc.Driver,
com.mysql.fabric.jdbc.FabricMySQLDriver)
*/
driversIterator.next();
}
}
约定:当服务的提供者,提供了服务接口(java.sql.Driver)的一种实现之后,在jar包的META-INF/services/目录里同时创建一个以服务接口命名的文件。该文件里就是实现该服务接口的具体实现类。而当外部程序装配这个模块的时候,就能通过该jar包META-INF/services/里的配置文件找到具体的实现类名,并装载实例化,完成模块的注入。
2、com.mysql.jdbc.Driver类的初始化
static {
try {
// registeredDrivers存储Dirver实例
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
3、获取数据库连接
for(DriverInfo aDriver : registeredDrivers) {
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
// 获取Connection
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());
}
}
JDK SPI简单使用
1、目录结构
2、示例代码
public interface HelloService {
String sayHello();
}
public class CHelloService implements HelloService {
@Override
public String sayHello() {
return "Welcome to C world";
}
}
public class JavaHelloService implements HelloService {
@Override
public String sayHello() {
return "Welcome to Java world";
}
}
3、com.codersm.study.jdk.spi.HelloService文件内容
com.codersm.study.jdk.spi.impl.CHelloService com.codersm.study.jdk.spi.impl.JavaHelloService
4、测试
@Test
public void testSpi() {
ServiceLoader<HelloService> loaders = ServiceLoader.load(HelloService.class);
for (HelloService loader : loaders) {
System.out.println(loader.sayHello());
}
}
以上所述就是小编给大家介绍的《JDK SPI源码详解》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 【zookeeper源码】启动流程详解
- 详解RunLoop之源码分析
- 详解CopyOnWrite容器及其源码
- React Scheduler 源码详解(1)
- React Scheduler 源码详解(2)
- PHP文件上传原理详解(附源码)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
内容创业:内容、分发、赢利新模式
张贵泉、张洵瑒 / 电子工业出版社 / 2018-6 / 49
越来越多的内容平台、行业巨头、资本纷纷加入内容分发的战争中,竞争非常激烈。优质的原创性内容将成为行业中最宝贵的资源。在这样的行业形势下,如何把内容创业做好?如何提高自身竞争力?如何在这场战争中武装自己?是每一位内容创业者都应该认真考虑的问题。 《内容创业:内容、分发、赢利新模式》旨在帮助内容创业者解决这些问题,为想要进入内容行业的创业者出谋划策,手把手教大家如何更好地进行内容创业,获得更高的......一起来看看 《内容创业:内容、分发、赢利新模式》 这本书的介绍吧!