maven 的runtime做用範圍java
其中JDBC驅動一般使用Class.forName("com.mysql.jdbc.Driver");來引入所須要的驅動。在編譯期間不用引入具體jdbc的具體實現類(無論mysql仍是oracle等)。因此JDBC包的scope應該設置爲runtime。mysql
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.driver.version}</version> <scope>runtime</scope> </dependency>
JDBC4以後使用SPI技術
也不用寫Class.forName了spring
Java API提供的使用ServiceLoader來實現的控制反轉工具類。用於從classpath中找到接口的實現類。sql
先定義一個接口數據庫
package ch.frankel.blog.serviceloader; public interface Foo { }
再定義兩個實現類(實現類多是其餘工程中定義的)apache
package ch.frankel.blog.serviceloader; public class FooImpl1 implements Foo { } public class FooImpl2 implements Foo { }
而後在Resources/META-INF下的service中新建一個與接口名對應的文件ch.frankel.blog.serviceloader.Foooracle
ch.frankel.blog.serviceloader.FooImpl1 ch.frankel.blog.serviceloader.FooImpl2
serviceLoader會從該文件中找到要加載的類。maven
public class JavaServiceLoaderTest { @Test public void java_service_loader_should_load_correct_implementations() { //Service.load會從ch.frankel.blog.serviceloader.Foo中找到要加載的類,而後加載。 ServiceLoader<Foo> loader = ServiceLoader.load(Foo.class); List<Foo> foos = new ArrayList<>(); loader.iterator().forEachRemaining(foos::add); assertThat(foos) .isNotNull() .isNotEmpty() .hasSize(2); } }
首先須要配置對應的factoryBeanide
@Configuration public class ServiceConfiguration { @Bean public ServiceListFactoryBean serviceListFactoryBean() { ServiceListFactoryBean serviceListFactoryBean = new ServiceListFactoryBean(); serviceListFactoryBean.setServiceType(Foo.class); return serviceListFactoryBean; } }
而後經過ServiceListFactoryBean就能夠找到接口對應的實現類。工具
@ContextConfiguration(classes = ServiceConfiguration.class) public class ServiceLoaderWithSpringTest extends AbstractTestNGSpringContextTests { @Autowired private ServiceListFactoryBean serviceListFactoryBean; @Test public void spring_service_loader_integration_should_load_correct_implementations() throws Exception { Object object = serviceListFactoryBean.getObject(); Assertions.assertThat(object) .isNotNull() .asList() .isNotEmpty() .hasSize(2); } }
spring 本身也提供了相似的工具類。使用起來更方便。
首先在META-INF下創建一個spring.factories文件。
ch.frankel.blog.serviceloader.Foo=ch.frankel.blog.serviceloader.FooImpl1,ch.frankel.blog.serviceloader.FooImpl2
直接指定接口對應的實現類。
而後經過Spring提供的靜態方法SpringFactoriesLoader就能夠直接使用了。
public class SpringFactoriesTest { @Test public void spring_factories_should_load_correct_implementations() { List<Foo> foos = SpringFactoriesLoader.loadFactories(Foo.class, null); assertThat(foos) .isNotNull() .isNotEmpty() .hasSize(2); } }
查看jdbc的代碼
//java.sql.DriverManager public class 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<String>() { public String run() { //從系統變量中獲取jdbc.drivers //System.getProperty 能夠在vm arguments中設置。java -Djdbc.drivers=xxxx 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; } //多個drivers? String[] driversList = drivers.split(":"); println("number of Drivers:" + driversList.length); for (String aDriver : driversList) { try { println("DriverManager.Initialize: loading " + aDriver); //仍是使用Class.forName來加載驅動 Class.forName(aDriver, true, ClassLoader.getSystemClassLoader()); } catch (Exception ex) { println("DriverManager.Initialize: load failed: " + ex); } } } }
在jdbc的實現包中META-INF下有java.sql.Driver文件。
com.mysql.jdbc.Driver com.mysql.fabric.jdbc.FabricMySQLDriver
文件中有兩個驅動名。
其實jdbc能夠同時管理多個驅動,(jdbc詳解:二、DriverManager管理多個數據庫驅動)
//org.apache.commons.logging.LogFactory#getFactory public static LogFactory getFactory() throws LogConfigurationException { ... // Second, try to find a service by using the JDK1.3 class // discovery mechanism, which involves putting a file with the name // of an interface class in the META-INF/services directory, where the // contents of the file is a single line specifying a concrete class // that implements the desired interface. if (factory == null) { if (isDiagnosticsEnabled()) { logDiagnostic( "[LOOKUP] Looking for a resource file of name [" + SERVICE_ID + "] to define the LogFactory subclass to use..."); } try { InputStream is = getResourceAsStream(contextClassLoader, SERVICE_ID); if( is != null ) { // This code is needed by EBCDIC and other strange systems. // It's a fix for bugs reported in xerces BufferedReader rd; try { rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); } catch (java.io.UnsupportedEncodingException e) { rd = new BufferedReader(new InputStreamReader(is)); } String factoryClassName = rd.readLine(); rd.close(); if (factoryClassName != null && ! "".equals(factoryClassName)) { if (isDiagnosticsEnabled()) { logDiagnostic( "[LOOKUP] Creating an instance of LogFactory class " + factoryClassName + " as specified by file '" + SERVICE_ID + "' which was present in the path of the context" + " classloader."); } factory = newFactory(factoryClassName, baseClassLoader, contextClassLoader ); } } else { // is == null if (isDiagnosticsEnabled()) { logDiagnostic( "[LOOKUP] No resource file with name '" + SERVICE_ID + "' found."); } } } catch( Exception ex ) { // note: if the specified LogFactory class wasn't compatible with LogFactory // for some reason, a ClassCastException will be caught here, and attempts will // continue to find a compatible class. if (isDiagnosticsEnabled()) { logDiagnostic( "[LOOKUP] A security exception occurred while trying to create an" + " instance of the custom factory class" + ": [" + ex.getMessage().trim() + "]. Trying alternative implementations..."); } ; // ignore } } ... }
其中也使用到SPI。從 META-INF/services/org.apache.commons.logging.LogFactory中找要加載的實現類(Apache Commons Logging 是如何決定使用哪一個日誌實現類的)。
而slf4j不是經過SPI來找實現類的。slf4j 1.7是經過找一個固定包下的固定類StaticLoggerBinder類(而SPI是找固定文件下的內容)。這個類定義在各個實現包中。
貌似slf4j 1.8 開始使用SPI了,以下圖。