DataSourceAutoConfiguration解析

2021-06-23 00:38:55 浏览数 (1)

DataSourceAutoConfiguration功能概况

•初始化 DataSourceProperties 配置文件•初始化数据源•执行 sql 文件•为数据源注册一个 DataSourcePoolMetadataProvider 实例

源码分析

代码语言:javascript复制
@Configuration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
//2.1 从配置文件中映射 DataSource 的值
@EnableConfigurationProperties(DataSourceProperties.class)
//2.4/2.5
@Import({ DataSourcePoolMetadataProvidersConfiguration.class,
      DataSourceInitializationConfiguration.class })
public class DataSourceAutoConfiguration {

   //2.2 
   @Configuration
   //判断是否引入 内置数据库:H2,DERBY,HSQL
   @Conditional(EmbeddedDatabaseCondition.class)
   //如果这是没有DataSource/XADataSource 对应的 BeanDefinition,就通过导入 EmbeddedDataSourceConfiguration.class 来,配置内置数据库对应的数据源!!
   @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
   @Import(EmbeddedDataSourceConfiguration.class)
   protected static class EmbeddedDatabaseConfiguration {}

   //2.3 
   @Configuration
   //判断是否引入依赖的数据源:HikariDataSource、tomcat.jdbc.pool.DataSource、BasicDataSource
   @Conditional(PooledDataSourceCondition.class)
   //如果这是没有DataSource/XADataSource 对应的 BeanDefinition,就通过以下属性的配置文件,配置数据源!!
   //配置数据源的时候,如果没有指定一些数据库的参数,就会报错哦
   @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
   @Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
         DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.Generic.class,
         DataSourceJmxConfiguration.class })
   protected static class PooledDataSourceConfiguration {}

   //详细说明见 2.3
   static class PooledDataSourceCondition extends AnyNestedCondition {}
   //详细说明见 2.2
   static class EmbeddedDatabaseCondition extends SpringBootCondition {}

0 人点赞