spring boot source code for datasource creation

GionJh :

I'm trying to go through spring-boot sources to gain a bit more confidence about its inner workings.

For example:
When you use following properties:

spring.datasource.url=jdbc:h2:./data/test
spring.datasource.username=sa
spring.datasource.password=as

spring boot will try to guess the jdbc driver you need by parsing spring.datasource.url property, and look in the classpath for the desired driver.

We can deduce this behaviour by reading Spring documentation:

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.

I think the code responsible for that could be found somewhere around here.

But I seem not to be able to find it.

Can you help me shed some light on that ?

Karol Dowbecki :

The JDBC driver class name is governed by the constants in the DatabaseDriver enum. See how this enum is used to understand where the datasource URL property is parsed e.g. in DataSourceConfiguration you will find:

DatabaseDriver databaseDriver = DatabaseDriver
                .fromJdbcUrl(properties.determineUrl());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95161&siteId=1