Skip to content

How to resolve 'Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource' in SpringBoot apps

Problem

When developing Spring Boot JDBC applications, you might encounter the following error:

Terminal window
> 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 applications use 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

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 the following lines to your application.properties in the src/main/resources directory:

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

All the code is uploaded to GitHub. You can download the example code here.

Summary

This post walked through resolving the “Failed to bind properties” error in Spring Boot applications. The key steps involve ensuring the MySQL driver is correctly added to the project dependencies and specifying the driver class name in the application properties. By following these steps, you can avoid common configuration issues and ensure your application starts successfully.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!