我正在尝试通过 spring-boot 源代码来获得对其内部工作的更多信心。
例如:
当您使用以下属性时:
spring.datasource.url=jdbc:h2:./data/test
spring.datasource.username=sa
spring.datasource.password=as
spring boot 将尝试通过解析 spring.datasource.url
属性来猜测您需要的 jdbc 驱动程序,并在类路径中查找所需的驱动程序。
我们可以通过阅读 Spring 文档来推断这种行为:
You often do not need to specify the driver-class-name, since Spring Boot can deduce it for most databases from the url.
For a pooling DataSource to be created, we need to be able to verify that a valid Driver class is available, so we check for that before doing anything. In other words, if you set spring.datasource.driver-class-name=com.mysql.jdbc.Driver, then that class has to be loadable.
我认为负责的代码可以在 here 附近的某处找到.
可是我好像找不到。
你能帮我解释一下吗?
最佳答案
JDBC 驱动程序类名由 DatabaseDriver
enum 中的常量控制.查看此枚举如何用于了解解析数据源 URL 属性的位置,例如在 DataSourceConfiguration
中,您会发现:
DatabaseDriver databaseDriver = DatabaseDriver
.fromJdbcUrl(properties.determineUrl());
关于java - 用于创建数据源的spring boot源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55476104/