Home > Software design >  Spring boot, Mysql and gradle base application "Error creating bean with name 'dataSourceS
Spring boot, Mysql and gradle base application "Error creating bean with name 'dataSourceS

Time:08-18

I am new in Spring framework, trying to connect my application with mysql database but getting following error, if I try to run the application by command

gradlew.bat bootRun 

from project root directory using terminal

error messgage:

  ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is 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 java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver

here is my gradle file:

plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id 'java'
}

group = 'com.rest-api-product-sample'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'  
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
springBoot {
 mainClass = 'com.restapiproductsample.apispringboot.ApiSpringBootApplication'
}
tasks.named('test') {
  useJUnitPlatform()
}
bootJar {
  enabled = false
}

jar {
 enabled = true
}

CodePudding user response:

Just add mysql-connector-java on your grade file and refresh gradle project to resolve that error. and the dependencies part on your file will be as following:

dependencies {
implementation 
'org.springframework.boot:spring-boot-starter-web'
implementation 
'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 
'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'mysql:mysql-connector-java'  
testImplementation 
'org.springframework.boot:spring-boot-starter-test'
}
  • Related