运行Spring Boot应用程序时出现DataSource错误

Asi*_*ick 6 spring spring-boot

我是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: {}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我也试过安迪威尔金森的 建议并补充道

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/mydb
Run Code Online (Sandbox Code Playgroud)

到我的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配置.

Atu*_*tul 7

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

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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

如果需要,您还可以下载我的示例应用程序的源代码进行比较 - 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/


Jan*_*nar 6

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

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
Run Code Online (Sandbox Code Playgroud)

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

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: {}
Run Code Online (Sandbox Code Playgroud)

说你正试图在某处自动装配数据源,但你没有配置(因为你排除了它)。只需删除自动装配的数据源,它应该可以工作。

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