Java Spring Boot:无法配置数据源:未指定“url”属性且无法配置嵌入式数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55481362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:12:37  来源:igfitidea点击:

Spring Boot: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

javaspringspring-boot

提问by Indrajith Ekanayake

Following error is thrown when I running my web app.

当我运行我的网络应用程序时抛出以下错误。

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 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 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

The description for the thrown error as follows,

抛出错误的描述如下,

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


    Action:

    Consider the following:<br>
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
<br>    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

After referring this answer, I know I have to do some changes with my pom.xmlfile. But I don't know what to change and even similar type questions on StackOverflow couldn't help me to sort this out.

参考此答案后,我知道我必须对我的pom.xml文件进行一些更改。但我不知道要改变什么,甚至 StackOverflow 上的类似问题也无法帮助我解决这个问题。

My pom.xmlas follows(when I creating project I have added "Web", "JPA", "MySQL" dependencies),

我的pom.xml如下(当我创建项目时,我添加了“Web”、“JPA”、“MySQL”依赖项),

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>SL2INDUSTRY</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>SL2INDUSTRY</name>
        <description>Demo project for Spring Boot</description>

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

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

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <version>2.1.3.RELEASE</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/com.fasterxml.Hymanson.dataformat/Hymanson-dataformat-xml -->
            <dependency>
                <groupId>com.fasterxml.Hymanson.dataformat</groupId>
                <artifactId>Hymanson-dataformat-xml</artifactId>
                <version>2.9.8</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    </project>

Plese note that I don't need to deal with H2 databases in this situation so H2 is not a solution for me.

请注意,在这种情况下我不需要处理 H2 数据库,因此 H2 不是我的解决方案。

Edits: application.propertiesfile

编辑: application.properties文件

spring.mvc.view.suffix=.jsp

采纳答案by Francesc Recio

If you read your trace of error:

如果您阅读了错误的踪迹:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 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 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

You will notice that he is trying to use hikari datasource to configure the connection to bd. What's going on?

您会注意到他正在尝试使用 hikari 数据源来配置与 bd 的连接。这是怎么回事?

You use spring boot 2. And by this version of spring boot has changed the default configuration.

你使用的是 spring boot 2。而这个版本的 spring boot 已经改变了默认配置。

As you can see at this address:

正如您在此地址中看到的:

https://www.baeldung.com/spring-boot-hikari#spring-boot-2

https://www.baeldung.com/spring-boot-hikari#spring-boot-2

In Spring Boot 2, Hikari is the default DataSource implementation.

This is what's changed from Spring Boot 1.x:

· the dependency to Hikari is now automatically included in spring-boot-starter-data-jpa

· the discovery algorithm that automatically determines a DataSource implementation now prefers Hikari over TomcatJDBC (see the reference manual).

Thus, we have nothing to do if we want to use Hikari in an application based on Spring Boot 2.x.

在 Spring Boot 2 中,Hikari 是默认的 DataSource 实现。

这是从 Spring Boot 1.x 更改的内容:

· 对 Hikari 的依赖现在自动包含在 spring-boot-starter-data-jpa 中

· 自动确定数据源实现的发现算法现在更喜欢 Hikari 而不是 TomcatJDBC(参见参考手册)。

因此,如果我们想在基于 Spring Boot 2.x 的应用程序中使用 Hikari,我们无所事事。

And the configuration of Hikari is diferent.

并且Hikari的配置是不同的。

Then, I imagine that you want use tomcat conection pool. You can to do:

然后,我想你想使用 tomcat 连接池。你可以这样做:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    <version>9.0.10</version>
</dependency>

This will exclude Hikari by default from your spring jpa configuration and then you will need tomcat-jdbc. The you can configure de datasource:

This simple approach allows us to get Spring Boot using a Tomcat connection pool without having to write a @Configuration class and programmatically define a DataSource bean.

It's also worth noting that in this case, we're using the H2 in-memory database. Spring Boot will autoconfigure H2 for us, without having to specify a database URL, user, and password.

We just need to include the corresponding dependency in the “pom.xml” file and Spring Boot will do the rest for us.

Alternatively, it's possible to skip the connection pool scanning algorithm that Spring Boot uses and explicitly specify a connection pooling datasource in the “application.properties” file, using the “spring.datasource.type” property:

默认情况下,这将从您的 spring jpa 配置中排除 Hikari,然后您将需要 tomcat-jdbc。您可以配置数据源:

这种简单的方法使我们可以使用 Tomcat 连接池获取 Spring Boot,而无需编写 @Configuration 类并以编程方式定义 DataSource bean。

还值得注意的是,在这种情况下,我们使用的是 H2 内存数据库。Spring Boot 会为我们自动配置 H2,而无需指定数据库 URL、用户和密码。

我们只需要在“pom.xml”文件中包含相应的依赖项,Spring Boot 会为我们完成剩下的工作。

或者,可以跳过 Spring Boot 使用的连接池扫描算法,并使用“spring.datasource.type”属性在“application.properties”文件中显式指定连接池数据源:

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
// other spring datasource properties
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
...

Here you have the complete reference to disable Hikari, and configure tomcat:

这里有禁用 Hikari 和配置 tomcat 的完整参考:

https://www.baeldung.com/spring-boot-tomcat-connection-pool

https://www.baeldung.com/spring-boot-tomcat-connection-pool

If you want use Hikari the configuration is diferent:

如果你想使用 Hikari,配置是不同的:

https://www.baeldung.com/spring-boot-hikari

https://www.baeldung.com/spring-boot-hikari

But one of these two options will fix your problem.

但是这两个选项之一将解决您的问题。

回答by Eder Rodrigues

There are a few possibilities for this, but the first time there are three things you need to do.

这有几种可能性,但第一次需要做三件事。

1 - Specific compatibles versions of your dependencies on your pom.xml

1 - 您对 pom.xml 的依赖项的特定兼容版本

2 - You need add database driverConnector on pom.xml

2 - 您需要在 pom.xml 上添加数据库 driverConnector

3 - Create an application.properties on 'src/main/resources/application.properties' directory and put your db configuration there.

3 - 在 'src/main/resources/application.properties' 目录上创建一个 application.properties 并将您的数据库配置放在那里。

application.properties (NOTE: Change de values with your database configuration)

application.properties(注意:使用您的数据库配置更改 de 值)

spring.jpa.hibernate.ddl-auto=create

spring.datasource.url=jdbc:mysql://localhost:3306/db_example

spring.datasource.username=user

spring.datasource.password=ThePassword

you can check a template here: https://spring.io/guides/gs/accessing-data-mysql/

您可以在此处查看模板:https: //spring.io/guides/gs/accessing-data-mysql/