} //一步步走,最终走到我们需要的地方 //com.zaxxer.hikari.HikariConfig#setDriverClassName //可以看到,这里并没有用spi组件,而是借用了spi的思想 publicvoidsetDriverClassName(String driverClassName){ if (this.sealed) { thrownew IllegalStateException("The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes."); } else { Class<?> driverClass = null; //获取当前的ClassLoader ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
try { if (threadContextClassLoader != null) { try { driverClass = threadContextClassLoader.loadClass(driverClassName); LOGGER.debug("Driver class {} found in Thread context class loader {}", driverClassName, threadContextClassLoader); } catch (ClassNotFoundException var6) { LOGGER.debug("Driver class {} not found in Thread context class loader {}, trying classloader {}", new Object[]{driverClassName, threadContextClassLoader, this.getClass().getClassLoader()}); } }
if (driverClass == null) { driverClass = this.getClass().getClassLoader().loadClass(driverClassName); LOGGER.debug("Driver class {} found in the HikariConfig class classloader {}", driverClassName, this.getClass().getClassLoader()); } } catch (ClassNotFoundException var7) { LOGGER.error("Failed to load driver class {} from HikariConfig class classloader {}", driverClassName, this.getClass().getClassLoader()); }
if (driverClass == null) { thrownew RuntimeException("Failed to load driver class " + driverClassName + " in either of HikariConfig class loader or Thread context classloader"); } else { try { driverClass.newInstance(); this.driverClassName = driverClassName; } catch (Exception var5) { thrownew RuntimeException("Failed to instantiate class " + driverClassName, var5); } } } }