数据源检测
@Configuration @Conditional(EmbeddedDatabaseCondition.class) @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) @Import(EmbeddedDataSourceConfiguration.class) protected static class EmbeddedDatabaseConfiguration { }具体的代码读者可翻阅相应的文档,这里作下总结
用户配置了spring.datasource.type属性或者classpath下存在springboot默认支持的数据源则该配置略过
1) com.zaxxer.hikari.HikariDataSource
2) org.apache.tomcat.jdbc.pool.DataSource
3) org.apache.commons.dbcp2.BasicDataSource
如果上述的条件不满足则会在classpath下找寻springboot默认支持的数据库驱动,存在则会创建SimpleDriverDataSource数据源用来创建数据库连接
1) H2 Database
2) Derby Database
3) HSQL Database
本例中引入mybatis-spring-boot-starter便会引入spring-jdbc包,则会采用HikariDataSource数据源来获取数据库连接
数据源创建
@Configuration @Conditional(PooledDataSourceCondition.class) @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) @Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class, DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class }) protected static class PooledDataSourceConfiguration { }根据用户配置的spring.datasource.type属性或者springboot默认支持的数据源(见上)来进行数据源对象的创建,具体的读者可自行阅读,本例中则会采取HikariDataSource数据源,并注入至bean工厂中
2.导入类解析
数据库连接池状态类初始化
@Configuration public class DataSourcePoolMetadataProvidersConfiguration { @Configuration @ConditionalOnClass(HikariDataSource.class) static class HikariPoolDataSourceMetadataProviderConfiguration { @Bean public DataSourcePoolMetadataProvider hikariPoolDataSourceMetadataProvider() { return (dataSource) -> { if (dataSource instanceof HikariDataSource) { return new HikariDataSourcePoolMetadata( (HikariDataSource) dataSource); } return null; }; } } }以HikariDataSource为例,则会创建HikariDataSourcePoolMetadata对象,主要是用来获取连接池的相关信息,看下DataSourcePoolMetadata接口就行,具体如下
public interface DataSourcePoolMetadata { /** * Return the usage of the pool as value between 0 and 1 (or -1 if the pool is not * limited). * <ul> * <li>1 means that the maximum number of connections have been allocated</li> * <li>0 means that no connection is currently active</li> * <li>-1 means there is not limit to the number of connections that can be allocated * </li> * </ul> * This may also return {@code null} if the data source does not provide the necessary * information to compute the poll usage. * @return the usage value or {@code null} */ Float getUsage(); /** * Return the current number of active connections that have been allocated from the * data source or {@code null} if that information is not available. * @return the number of active connections or {@code null} */ Integer getActive(); /** * Return the maximum number of active connections that can be allocated at the same * time or {@code -1} if there is no limit. Can also return {@code null} if that * information is not available. * @return the maximum number of active connections or {@code null} */ Integer getMax(); /** * Return the minimum number of idle connections in the pool or {@code null} if that * information is not available. * @return the minimum number of active connections or {@code null} */ Integer getMin(); /** * Return the query to use to validate that a connection is valid or {@code null} if * that information is not available. * @return the validation query or {@code null} */ String getValidationQuery(); /** * The default auto-commit state of connections created by this pool. If not set * ({@code null}), default is JDBC driver default (If set to null then the * java.sql.Connection.setAutoCommit(boolean) method will not be called.) * @return the default auto-commit state or {@code null} */ Boolean getDefaultAutoCommit(); }sql脚本执行加载
@Configuration @Import({ DataSourceInitializerInvoker.class, DataSourceInitializationConfiguration.Registrar.class }) class DataSourceInitializationConfiguration { }主要通过DataSourceInitializerInvoker类来进行sql脚本的执行加载,具体笔者就不贴代码了,作下简单的总结
1) 如果spring.datasource.schema属性已指定相应的sql文件,则优先读取,并支持classpath路径查找
2) 如果上述无配置,则默认读取classpath*:schema.sql/classpath*:schema-${platform}.sql文件(其中${platform}可用spring.datasource.platform指定)
3) 如果没有上述文件,则不执行
温馨提示:如果想在环境运行的时候执行相应的sql语句,则仍需要另外配置用户名(spring.datasource.schema-username)与密码(spring.datasource.schema-password),方可执行
小结