无法加载驱动程序类:com.mysql.jdbc.Driver 与 Gradle 和 Spring Boot

Posted

技术标签:

【中文标题】无法加载驱动程序类:com.mysql.jdbc.Driver 与 Gradle 和 Spring Boot【英文标题】:Cannot load driver class: com.mysql.jdbc.Driver with Gradle and Spring Boot 【发布时间】:2015-03-01 18:17:45 【问题描述】:

我的问题如下。我正在学习如何使用 JDBC、Gradle 和 Spring 框架(我是这些主题的新手)。我一直在尝试实现以下example,但有一个区别,mysql 数据库而不是 PostgreSQL。

就像我在标题中指定的那样,我的应用程序以以下错误结束Cannot load driver class: com.mysql.jdbc.Driver(帖子末尾的堆栈跟踪)。

当然,在发布这个问题之前我一直在谷歌搜索和阅读,我发现com.mysql.jdbc.Driver 应该使用程序中的加载程序加载,或者也可以使用 Gradle 构建脚本来完成。

我的问题如下:

    为什么作者的示例可以在没有加载器的情况下工作(在程序中或在构建脚本中) 如果加载器是绝对必要的,你能简单解释一下如何实现它吗?

我修改的build.gradle文件:

buildscript  
    repositories  
        maven  url "http://repo.spring.io/libs-snapshot"  
     
    dependencies  
        classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6") 
        classpath 'mysql:mysql-connector-java:5.1.34'  
     
 

apply plugin: "java" 
apply plugin: "spring-boot" 

buildDir = "out" 

jar  
    baseName = "sb-jdbc" 
    version = "0.1" 
 

repositories  
    mavenCentral() 
    maven  url "http://repo.spring.io/libs-snapshot"  
 

dependencies  
    def springBootVersion = '1.0.0.RC1'
    compile("org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion")
    compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
    compile("mysql:mysql-connector-java:5.1.34")

    testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
    testCompile("junit:junit:4.11")
 

我的application.properties 文件:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/:3306/******
spring.datasource.username=root
spring.datasource.password=******

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy

堆栈跟踪(未满):

2015-01-03 20:09:20.203  WARN 10298 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.springframework.jdbc.core.JdbcTemplate demo.UserRepository.jdbc; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
    at demo.WebBindGradleApplication.main(WebBindGradleApplication.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected org.springframework.jdbc.core.JdbcTemplate demo.UserRepository.jdbc; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 22 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:371)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:530)
    ... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 44 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:602)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:530)
    ... 46 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:591)
    ... 58 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
    at org.springframework.util.Assert.state(Assert.java:385)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:122)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:116)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 59 common frames omitted

2015-01-03 20:09:20.254  INFO 10298 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2015-01-03 20:09:20.308  INFO 10298 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-starter-web-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-starter-jdbc-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-starter-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-starter-tomcat-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/jackson-databind-2.4.4.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/hibernate-validator-5.1.3.Final.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-core-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-web-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-webmvc-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-jdbc-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/tomcat-jdbc-8.0.15.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-tx-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-autoconfigure-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-boot-starter-logging-1.2.0.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/snakeyaml-1.14.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/tomcat-embed-core-8.0.15.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/tomcat-embed-el-8.0.15.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/tomcat-embed-logging-juli-8.0.15.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/tomcat-embed-websocket-8.0.15.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/jackson-annotations-2.4.0.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/jackson-core-2.4.4.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/validation-api-1.1.0.Final.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/jboss-logging-3.1.3.GA.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/classmate-1.0.0.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-aop-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-beans-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-context-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/spring-expression-4.1.3.RELEASE.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/tomcat-juli-8.0.15.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/jcl-over-slf4j-1.7.7.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/jul-to-slf4j-1.7.7.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/log4j-over-slf4j-1.7.7.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/logback-classic-1.1.2.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/aopalliance-1.0.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/slf4j-api-1.7.7.jar!/, jar:file:/home/pucek/Documents/workspace-sts-3.6.3.RELEASE/WebBindGradle/build/libs/demo-0.0.1-SNAPSHOT.jar!/lib/logback-core-1.1.2.jar!/]
2015-01-03 20:09:20.308  INFO 10298 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)


2015-01-03 20:09:20.310 ERROR 10298 --- [           main] o.s.boot.SpringApplication               : Application startup failed

提前感谢您提供任何建议、解释、共享示例等! 干杯!

编辑#1

正如 Kamoor 先生好心建议的那样,这可能是缺少本地 *jar 文件的问题。我怀疑 Gradle 会自动从存储库下载它,以防万一我手动添加它(我在 STS 上工作)。在这里我们可以看到mysql-connector-java jar 在那里。

我很抱歉那些屏幕截图,我只是不知道其他方式来表明我添加了它(我不习惯 Eclipse)。

【问题讨论】:

要更新到新版spring boot 1.2.0.RELEASE 刚刚尝试使用'1.2.0.RC1' 并得到相同的结果。谢谢你的建议。 RC1 不是 GA 版本。使用释放。并将该版本用于 Spring Boot 插件(为什么你有 0.5.0.x?)。另外我不明白为什么需要 mysql 作为构建类路径依赖项,所以这看起来很奇怪。 您不必指定驱动程序,因为使用 JDBC4 驱动程序它会自动注册,因此无需自己加载。旧的使用 Class.forName 加载和读取是 JDBC 之前的,现在应该避免。 感谢您的快速反馈。 @DaveSyer,我使用旧版本的 Spring Boot 插件只是因为新版本无法构建(我一直在网络上查看大量示例,这只是一个成功构建的示例)。我会尝试用最新的 Spring Boot 插件来构建它。 【参考方案1】:

我通过反复试验解决了这个问题。对我来说,它是删除驱动程序参考表单 application.properties 文件。所以我的 application.properties 文件看起来像这样,错误立即消失了:

spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root 
spring.datasource.password = fake
#spring.datasource.driver-class-name = com.mysql.jdbc.Driver 
mybatis.config=mybatis-config.xml
mybatis.check-config-location=true

我认为这与您在嵌入式 Tomcat 中而不是在应用程序代码本身中需要该驱动程序的可能性有关

【讨论】:

仅供下一个读者,我有类似的问题,是由于下载mysql jar时gradle失败引起的。用的是2.11,改成2.14.1,gradle下载了jar。 与gradle或maven无关,我面临同样的问题,解决方案与您所做的相同。删除驱动引用,在代码中实例化 Class.forName("com.mysql.jdbc.Driver")。 驱动程序用于其他供应商。您不能同时使用两个驱动程序。【参考方案2】:

我知道我的情况很特殊,但我应该让其他人知道我的发现。

就我而言, - 我已经用 brew 安装了 gradle,gradle 版本是 2.1 (即使是现在) - 问题:com.mysql.jdbc.Driver 类未找到

我尝试了很多方法,但最终我解决了这个问题:升级 Gradle(例如 3.3。)

升级等级 gradle init 再次(删除 build.gradle 和 settings.gradle) 将旧版本/设置中的所有内容复制到新版本/设置中

它成功了。

【讨论】:

【参考方案3】:

@EnableAutoConfiguration 将确保加载适当的 bean,这里驱动程序将由 Spring 引导加载。见documentation

在您的情况下,我没有看到 MySQL 驱动程序 jar 文件不包含在类路径中,因此 Spring 无法找到 Driver 类。在日志中搜索“ClasspathLoggingApplicationListener”可以看到所有的jar包都包含在运行应用中。

【讨论】:

我以为 Gradle 会像下载 Spring 依赖项一样下载它。不幸的是,ClasspathLoggingApplicationListener 不存在于 Gradle 生成的日志中。非常感谢您的快速反馈! :) 它在类路径 (build.gradle) 上,看起来像在存储库中,所以您确定这是问题所在? 我使用 STS 手动添加了它,还用截图更新了我的帖子,不幸的是它没有解决问题..【参考方案4】:

将此添加到您的 pom.xml:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>

然后检查 application.properties

【讨论】:

他用的是Gradle,什么POM.xml?【参考方案5】:

它对我有用

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>

【讨论】:

以上是关于无法加载驱动程序类:com.mysql.jdbc.Driver 与 Gradle 和 Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章

无法加载驱动程序类:com.mysql.jdbc.Driver 与 Gradle 和 Spring Boot

从 2.6 升级到 3.7 时,BIRT JDBCException“无法加载 JDBC 驱动程序类:com.mysql.jdbc.Driver”

Oozie - 运行 sqoop 时出现异常:无法加载数据库驱动程序类:com.mysql.jdbc.Driver

加载类“com.mysql.jdbc.Driver”。这已被弃用。新的驱动程序类是`com.mysql.cj.jdbc.Driver' [重复]

java原本ok的后来却运行出现错误: 找不到或无法加载主类 com.mysql.jdbc.DocsConnectionPropsHelper

无法载入com.mysql.jdbc.Driver驱动程序! 无法连接数据库!