【问题标题】:Cant run Spring Boot application reason Failed to load driver class com.mysql.jdbc.Driver无法运行 Spring Boot 应用程序原因 无法加载驱动程序类 com.mysql.jdbc.Driver
【发布时间】:2019-02-07 23:39:23
【问题描述】:

我在项目中添加 Flyway 时出现此错误。 现在我无法运行该应用程序,因为出现此错误。我尝试更改 mysql 版本,但这没有帮助。此外,我删除了 V1__Init_DB.sql 和 V2__Add_admin.sql(为 Flyway 创建的文件),但应用程序仍未运行。我在 stackoverflow 和其他网站上阅读了很多答案,但仍然没有解决这个问题的方法。

ERROR 12860 --- [  restartedMain] com.zaxxer.hikari.HikariConfig           : Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader jdk.internal.loader.ClassLoaders$AppClassLoader@5fa7e7ff
WARN 12860 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration$$EnhancerBySpringCGLIB$$3c93ce13]: Constructor threw exception; 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 org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource
INFO 12860 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
INFO 12860 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
ERROR 12860 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

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

    Property: driverclassname
    Value: com.mysql.jdbc.Driver
    Origin: "driverClassName" from property source "source"
    Reason: Unable to set value for property driver-class-name

Action:

Update your application's configuration

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>coding</groupId>
    <artifactId>application</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>

        <groupId>org.springframework</groupId>
        <artifactId>gs-mysql-data</artifactId>
        <version>0.1.0</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- database -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>

        </dependency>

        <dependency>
             <groupId>org.flywaydb</groupId>
             <artifactId>flyway-core</artifactId>
                <version>5.1.4</version>
         </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.170</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

        <!-- security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- spring boot mail -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>5.1.4</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/appdatabase?true&useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.generate-ddl=false
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate
spring.flyway.baseline-on-migrate=true

spring.freemarker.expose-request-attributes=true

upload.path=E:/Programming/Projects/Java/appdatabase/uploads

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.mail.host=smtp.gmail.com
spring.mail.username=mail
spring.mail.password=pass
spring.mail.port=465
spring.mail.protocol=smtps
mail.debug=true

spring.flyway.url: jdbc:mysql://localhost:3306/appdatabase?true&useSSL=false
spring.flyway.user: root
spring.flyway.password: admin
spring.fly.setBaselineOnMigrate(true)
spring.com.mysql.jdbc.Driver
spring.com.mysql.cj.jdbc.Driver

我将 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 放入 application.properties 并删除了我上面描述的错误,但现在我有另一个错误:

WARN 7588 --- [  restartedMain] org.flywaydb.core.Flyway                 : Flyway.setCallbacks(FlywayCallback) has been deprecated and will be removed in Flyway 6.0. Use Flyway.setCallbacks(Callback) instead.
INFO 7588 --- [  restartedMain] o.f.core.internal.util.VersionPrinter    : Flyway Community Edition 5.1.4 by Boxfuse
INFO 7588 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
INFO 7588 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
INFO 7588 --- [  restartedMain] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://localhost:3306/twitterclone (MySQL 8.0)
WARN 7588 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Validate failed: Detected failed migration to version 1 (Init DB)
INFO 7588 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
INFO 7588 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
INFO 7588 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]

【问题讨论】:

    标签: java mysql sql spring spring-boot


    【解决方案1】:

    从错误消息看来,您可能只是在application.properties 中遗漏了以下内容:

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    引用the official documentation:

    您至少应该通过设置 spring.datasource.url 属性来指定 URL。否则,Spring Boot 会尝试自动配置嵌入式数据库。

    您通常不需要指定驱动程序类名称,因为 Spring Boot 可以从 url 为大多数数据库推断它。

    对于要创建的池化 DataSource,我们需要能够验证有效的 Driver 类是否可用,因此我们在做任何事情之前都会检查它。换句话说,如果您设置spring.datasource.driver-class-name=com.mysql.jdbc.Driver,那么该类必须是可加载的。

    【讨论】:

    • 我将 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 放入 application.properties,现在我有另一个错误 Error created bean with name 'flywayInitializer' defined in class路径资源[org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]:init方法调用失败;嵌套异常是 org.flywaydb.core.api.FlywayException
    • 您应该将整个日志发布到某个 pastebin - 但这是另一个问题
    猜你喜欢
    • 2018-11-13
    • 2016-01-12
    • 2015-03-01
    • 1970-01-01
    • 2016-08-18
    • 2012-04-23
    • 1970-01-01
    • 2012-03-18
    相关资源
    最近更新 更多