How to configure mutiple DataSource with driver-class in Spring?

membersound :

Why is the driverclass missing in the following configuration?

spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable
spring.datasource.testdb.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.testdb.username=test
spring.datasource.testdb.password=test


@Configuration
public class DataSourceConfig {
    @ConfigurationProperties(prefix = "spring.datasource.testdb")
    @Primary
    public DataSource dataSourceTest() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public JdbcTemplate jdbcTemplateTest() {
        return new JdbcTemplate(dataSourceTest());
    }

    //secondary db config to follow
}

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
    </dependency>
</dependencies>

Result:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class

This is strange, because I can even step into the org.mariadb.jdbc.Driver class, so it is clearly on classpath.

Ryan Stuetzer :

Looks like you just need to add the @Bean annotation to dataSourceTest().

@Bean
@ConfigurationProperties(prefix = "spring.datasource.testdb")
@Primary

Also, for the Hikari connection pool (which is the default connection pool), the url property is jdbc-url, not url. So change

spring.datasource.testdb.url=jdbc:mariadb://localhost/mytable

to

spring.datasource.testdb.jdbc-url=jdbc:mariadb://localhost/mytable

For more info, and other possible solutions see: After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName

Hope this helps.

Guess you like

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