spring boot auto如何配置特殊数据源的驱动程序?
how does spring boot auto configure the driver of a special datasource?
没有spring启动,我们必须指定数据源的详细信息,对吗?
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}
有了spring boot,我们甚至不需要做任何事情,我知道spring boot会检测是否有包含数据源的jar来决定是创建数据源bean还是not.I 查看来自 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
的源代码:
@Configuration(proxyBeanMethods = false)
@Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })
protected static class PooledDataSourceConfiguration {
}
但我的问题是 spring 引导如何知道驱动程序 class 或 url 以及每个不同数据库的其他信息?我无法从 spring-boot-autoconfigure-2.5.0.jar
中找到任何规范
来自文档中的DataSource Configuration:
Spring Boot can deduce the JDBC driver class for most databases from the URL. If you need to specify a specific class, you can use the spring.datasource.driver-class-name
property.
所以开始时不要配置任何东西,只需将 JDBC 驱动程序放在类路径中即可。如果不行,您可以手动配置。
没有spring启动,我们必须指定数据源的详细信息,对吗?
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}
有了spring boot,我们甚至不需要做任何事情,我知道spring boot会检测是否有包含数据源的jar来决定是创建数据源bean还是not.I 查看来自 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
的源代码:
@Configuration(proxyBeanMethods = false)
@Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })
protected static class PooledDataSourceConfiguration {
}
但我的问题是 spring 引导如何知道驱动程序 class 或 url 以及每个不同数据库的其他信息?我无法从 spring-boot-autoconfigure-2.5.0.jar
中找到任何规范
来自文档中的DataSource Configuration:
Spring Boot can deduce the JDBC driver class for most databases from the URL. If you need to specify a specific class, you can use the
spring.datasource.driver-class-name
property.
所以开始时不要配置任何东西,只需将 JDBC 驱动程序放在类路径中即可。如果不行,您可以手动配置。