【问题标题】:DataSource Error while running Spring Boot application运行 Spring Boot 应用程序时出现数据源错误
【发布时间】:2018-06-04 22:55:10
【问题描述】:

我是 Spring boot 的新手。我收到此错误

Cannot determine embedded database driver class for database type NONE

每当尝试运行我的 spring-boot 启动 Web 应用程序时(我正在尝试测试执行器和 hal 浏览器)。在过去八小时左右的时间里,我尝试了一些关于 google/stackoverflow 的建议。但似乎对我不起作用。我仍然不断收到另一个错误。

第一次尝试: 我遵循journaldev中提到的两种方法

如果我使用 第一种方法,即注释我的主应用程序类 使用@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }),我收到此错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

如果我使用第二种方法,我仍然会得到另一个错误:

Binding to target [Bindable@7c551ad4 type = com.zaxxer.hikari.HikariDataSource, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

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

我还尝试了 Andy Wilkinson 的 suggestion 并添加了

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mydb

到我的 application.properties 文件,但我收到此错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor 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.jdbc.Driver

我还尝试提供用户名和密码(不确定是否需要,因为我没有尝试访问我的数据库),但对我不起作用。如果需要,我也可以提供我的 pom 配置。

【问题讨论】:

  • 你的pom中有mysql依赖吗?
  • 是的,我可以在有效的 pom.xml 中看到依赖关系。仅供参考,我使用的是 2.0.0.M3 版本的 spring-boot-starter-parent
  • 我相信应该像这样指定驱动程序类名称spring.datasource.driver-class-name=
  • 我仍然收到错误Cannot load driver class: com.mysql.jdbc.Driver
  • 显然你需要一个DataSource,因为你配置了一些需要它的东西。添加适当的数据源。您不能添加任意一个(就像您尝试使用 MySQL 驱动程序一样,因为它需要一个正在运行的 MySQL 实例并且需要适当的配置)。如果您不需要它,请删除需要数据库的依赖项(如 JPA 等),如果需要添加驱动程序(如内存数据库的 H2)或与您要连接的数据库匹配的驱动程序。

标签: spring spring-boot


【解决方案1】:

你说你不需要访问数据库所以你应该可以使用

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })

并删除所有包含数据源的自动装配。 你得到的异常

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

说您正在尝试在某处自动连接数据源,但您没有配置数据源(因为您已将其排除)。只需删除自动装配的数据源,它应该可以工作。

如果您确实需要使用数据库,那么 mysql 驱动程序似乎有问题 - 确保您添加了一个作为依赖项。

【讨论】:

  • 我尝试从自动配置中排除 DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class 并运行我的应用程序。它给了我这个错误:java.lang.ClassCastException: org.springframework.boot.context.event.ApplicationReadyEvent cannot be cast to org.springframework.boot.web.context.WebServerInitializedEvent 这就是你的意思吗?但我不确定如何删除 All 数据源自动装配。很抱歉@Janar 回复晚了。这里经常断电。
  • 这是一个完全不同的错误,与数据源无关(这可能意味着您修复了数据源的问题),但代码中似乎还有其他错误
  • 谢谢。这似乎是永无止境的错误循环。我不喜欢问任何萌问题,因为这似乎是在浪费你和其他人研究这个的时间(感谢 spring boot) .我只是在学习这个春季靴子时遇到了糟糕的经历。 Spring boot starter-parent 似乎从来都不是稳定的。有时我会看到降级的建议,有时会看到升级的建议。似乎与 Spring boot 本身的使命完全相反。
【解决方案2】:

以下配置对我来说非常好 -

application.properties -

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rolb
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.initialize=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

pom.xml -

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <!-- <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency> -->

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


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

    </dependencies>

如果你愿意,你也可以下载我的示例应用程序的源代码进行比较 - https://github.com/atulajoshi24/springboot-rest.git

相同的相关博客文章 - http://thejavatechie.com/2017/12/21/single-page-application-using-spring-boot-rest-and-angular-1-part-1/

【讨论】:

  • 非常感谢阿图尔。我通过将spring-boot-starter-parent 降级为2.0.0.M3 解决了这个问题。早些时候它是2.0.0.M5(我在原始帖子中错误地将版本引用为 2.0.0.M3)Spring Boot 版本似乎是实验性的。当我仍然需要处理版本控制时,这有什么意义。我正在保存您的建议以供将来参考。我刚刚开始学习 Spring Boot。
  • 这真的很奇怪。因为对于 datasource ,您无论如何都必须添加额外的外部依赖项。可能是 spring boot 版本 2.0.0.M5 可能无法很好地与数据源一起使用?另外,您似乎使用的是spring boot 2,您可以尝试使用spring boot 1.4版吗?
  • 我也确实包含了 h2 依赖项。只是不记得记下哪个步骤实际解决了这个问题。
【解决方案3】:

你需要在 pom 文件中添加 JDBC DRIVER 依赖,然后应该可以工作

【讨论】:

    【解决方案4】:

    确保 pom.xml 中有 mysql 依赖项并注释 h2 依赖项

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

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2017-02-18
      相关资源
      最近更新 更多