springboot-how to solve 'Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource' in SpringBoot apps

Problem

When we develop spring boot jdbc applications , sometimes, we got this problem:


> Task :app3:bootRun FAILED

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-11-21 16:32:15.155  INFO 62948 --- [           main] com.bswen.app3.Main                      : Starting Main on MBP-bswen with PID 62948 (/Users/bswen/bswen-springboot23/app3/build/classes/java/main started by bswen in /Users/bswen/bswen-springboot23/app3)
2020-11-21 16:32:15.157  INFO 62948 --- [           main] com.bswen.app3.Main                      : No active profile set, falling back to default profiles: default
2020-11-21 16:32:15.502  INFO 62948 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2020-11-21 16:32:15.517  INFO 62948 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 10ms. Found 0 JDBC repository interfaces.
2020-11-21 16:32:15.708 ERROR 62948 --- [           main] com.zaxxer.hikari.HikariConfig           : Failed to load driver class com.mysql.cj.jdbc.Driver from HikariConfig class classloader sun.misc.Launcher$AppClassLoader@2a139a55
2020-11-21 16:32:15.710  WARN 62948 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource
2020-11-21 16:32:15.716  INFO 62948 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-21 16:32:15.724 ERROR 62948 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:

    Property: driver-class-name
    Value: com.mysql.cj.jdbc.Driver
    Origin: "driverClassName" from property source "source"
    Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader

Action:

Update your application's configuration


Execution failed for task ':app3:bootRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

Environment

  • mysql
  • spring boot 2.3

Reason

Spring boot application uses the HikariDataSource as the default connection pool, it expects the mysql driver class name ‘com.mysql.cj.jdbc.Driver’, but this class is not found in the classpath.

Solution

Step 1: Add mysql dependency in your build.gradle

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    runtimeOnly 'mysql:mysql-connector-java'
}

Step 2: Specify the mysql jdbc driver class name in your spring boot application properties

Add following lines to your application.properties in src/main/resources directory.

spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver

All the code is upload to github, you can download the example code here.