Spring Boot(3):加載DataSource過程的源碼分析及yml中DataSource的配置
Spring Boot實現了自動加載DataSource及相關配置。當然,使用時加上@EnableAutoConfiguration註解是必須的。下面就是對這一部分的源碼分析。
(1)Spring Boot啟動後會調用org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration。下面是部分源碼。
1 @Configuration 2 @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) 3 @EnableConfigurationProperties(DataSourceProperties.class) 4 @Import({ DataSourcePoolMetadataProvidersConfiguration.class, 5 DataSourceInitializationConfiguration.class }) 6 public class DataSourceAutoConfiguration { 7 8 @Configuration 9 @Conditional(EmbeddedDatabaseCondition.class) 10 @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) 11 @Import(EmbeddedDataSourceConfiguration.class) 12 protected static class EmbeddedDatabaseConfiguration { 13 } 14 15 @Configuration 16 @Conditional(PooledDataSourceCondition.class) 17 @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })18 @Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class, 19 DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.Generic.class, 20 DataSourceJmxConfiguration.class }) 21 protected static class PooledDataSourceConfiguration { 22 } 23 ...... 24 }
我們從中可以看出,DataSourceAutoConfiguration中有兩個嵌套類,一個是EmbeddedDatabaseConfiguration,另一個是PooledDataSourceConfiguration。
EmbeddedDatabaseConfiguration表示已經嵌入Spring Boot的DataSource,除了Maven中加入相應的Driver,可以不做其他額外配置就能使用。從EmbeddedDatabaseType類可以看出,Spring Boot的內嵌DataSource支持HSQL,H2,DERBY這三種DB。
PooledDataSourceConfiguration表示Spring Boot還支持一些實現Pool的DataSource。從org.springframework.boot.jdbc.DataSourceBuilder中可以看出,當前版本的Spring Boot(2.0)只支持com.zaxxer.hikari.HikariDataSource,org.apache.tomcat.jdbc.pool.DataSource,org.apache.commons.dbcp2.BasicDataSource。其中,性能更加優秀的HikariDataSource是Spring Boot的默認選擇(DataSourceBuilder中DATA_SOURCE_TYPE_NAMES[0]=com.zaxxer.hikari.HikariDataSource)。所以,當application.yml文件中做如下配置時,Spring Boot默認使用HikariDataSource數據庫連接池。
spring: datasource: url: jdbc:mysql://localhost:3306/sas username: root password: **** driver-class-name: com.mysql.jdbc.Driver #type: com.zaxxer.hikari.HikariDataSource
(2)我們以HikariDataSource舉例,接下來調用PooledDataSourceConfiguration中org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration抽象類的Hikari嵌套類(DataSourceConfiguration抽象類的一個實現類)。
1 abstract class DataSourceConfiguration { 2 3 @SuppressWarnings("unchecked") 4 protected <T> T createDataSource(DataSourceProperties properties, 5 Class<? extends DataSource> type) { 6 return (T) properties.initializeDataSourceBuilder().type(type).build(); 7 } 8 9 /* Omit Tomcat Pool DataSource configuration.*/ 10 /** 11 * Hikari DataSource configuration. 12 */ 13 @ConditionalOnClass(HikariDataSource.class) 14 @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true) 15 static class Hikari extends DataSourceConfiguration { 16 17 @Bean 18 @ConfigurationProperties(prefix = "spring.datasource.hikari") 19 public HikariDataSource dataSource(DataSourceProperties properties) { 20 HikariDataSource dataSource = createDataSource(properties, 21 HikariDataSource.class); 22 if (StringUtils.hasText(properties.getName())) { 23 dataSource.setPoolName(properties.getName()); 24 } 25 return dataSource; 26 } 27 } 28 /* Omit DBCP DataSource configuration.*/ 29 }
我們從黃色部分可以看出,當application.yml文件中配置spring.datasource.type=com.zaxxer.hikari.HikariDataSource時,會使用HikariDataSource作為數據庫連接池。它會
Spring Boot(3):加載DataSource過程的源碼分析及yml中DataSource的配置