Java中SPI機(jī)制的實(shí)現(xiàn)詳解
簡(jiǎn)介
SPI(Service Provider Interface),是 JDK 內(nèi)置的一種服務(wù)提供發(fā)現(xiàn)機(jī)制,可以用來啟用框架擴(kuò)展和替換組件,核心思想是解耦。比如java.sql.Driver接口,其他不同廠商可以針對(duì)同一接口做出不同的實(shí)現(xiàn),MySQL 和 PostgreSQL 都有不同的實(shí)現(xiàn)提供給用戶。
當(dāng)服務(wù)提供者提供了一種接口的實(shí)現(xiàn)后,需要在 classpath 下的 META-INF/services/ 目錄下創(chuàng)建一個(gè)以服務(wù)接口命名的文件,文件的內(nèi)容就是接口的具體實(shí)現(xiàn)類。
當(dāng)使用該服務(wù)時(shí),掃描 META-INF/services/ 下配置文件,就可以加載類使用該服務(wù)。JDK 中查找服務(wù)的工具類是:java.util.ServiceLoader。
以 MySQL 驅(qū)動(dòng)為例:

源碼實(shí)現(xiàn)
以上面連接數(shù)據(jù)庫(kù)作為示例,看看 java8 SPI 怎么實(shí)現(xiàn)的。
入口在 java.sql.DriverManager 類中:
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
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;
}
// 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() {
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(":");
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);
}
}
}
上面的代碼主要步驟是:
- 從系統(tǒng)變量中獲取有關(guān)驅(qū)動(dòng)的定義。
- 使用 SPI 來獲取驅(qū)動(dòng)的實(shí)現(xiàn)。
- 遍歷使用 SPI 獲取到的具體實(shí)現(xiàn),實(shí)例化各個(gè)實(shí)現(xiàn)類。
- 根據(jù)第一步獲取到的驅(qū)動(dòng)列表來實(shí)例化具體實(shí)現(xiàn)類。
主要關(guān)注第2,3步,第 2 步使用 SPI 獲取驅(qū)動(dòng)的實(shí)現(xiàn),對(duì)應(yīng)實(shí)現(xiàn):
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
這里沒有去 META-INF/services 目錄下查找配置文件,也沒有加載具體實(shí)現(xiàn)類,只是封裝了接口類型和類加載器,并初始化了一個(gè)迭代器。
接著看第三步,遍歷使用 SPI 獲取到的具體實(shí)現(xiàn),實(shí)例化各個(gè)實(shí)現(xiàn)類,對(duì)應(yīng)的代碼如下:
// 獲取迭代器
Iterator<Driver> driversIterator = loadedDrivers.iterator();
// 遍歷所有的驅(qū)動(dòng)實(shí)現(xiàn)
while(driversIterator.hasNext()) {
driversIterator.next();
}
在遍歷的時(shí)候,首先調(diào)用driversIterator.hasNext()方法,這里會(huì)搜索 classpath 下以及jar包中所有的META-INF/services目錄下的java.sql.Driver文件,并找到文件中的實(shí)現(xiàn)類的名字,此時(shí)并沒有實(shí)例化具體的實(shí)現(xiàn)類。
然后是調(diào)用driversIterator.next();方法,此時(shí)就會(huì)根據(jù)驅(qū)動(dòng)名字具體實(shí)例化各個(gè)實(shí)現(xiàn)類了。具體的掃描加載源碼見java.util.ServiceLoader 方法。
以上就是Java中SPI機(jī)制的實(shí)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于Java SPI機(jī)制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Java驗(yàn)證jwt token代碼實(shí)例
這篇文章主要介紹了基于Java驗(yàn)證jwt token代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SpringBoot+微信小程序?qū)崿F(xiàn)文件上傳與下載功能詳解
這篇文章主要為大家介紹了SpringBoot整合微信小程序?qū)崿F(xiàn)文件上傳與下載功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-03-03
詳解Spring學(xué)習(xí)總結(jié)——Spring實(shí)現(xiàn)AOP的多種方式
這篇文章主要介紹了詳解Spring學(xué)習(xí)總結(jié)——Spring實(shí)現(xiàn)AOP的多種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
java對(duì)象池管理方式common-pool2使用
這篇文章主要為大家介紹了java對(duì)象池common-pool2使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
MyBatis異常-Property ''configLocation'' not specified, using d
今天小編就為大家分享一篇關(guān)于MyBatis異常-Property 'configLocation' not specified, using default MyBatis Configuration,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03

