Spring Boot:无法配置数据源:未指定“url"属性且无法配置嵌入式数据源 [英] Spring Boot: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

查看:65
本文介绍了Spring Boot:无法配置数据源:未指定“url"属性且无法配置嵌入式数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration"的bean时出错:表达了不满意的依赖通过构造函数参数0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中定义名称为dataSource"的 bean 创建错误:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.zaxxer.hikari.HikariDataSource]:工厂方法dataSource"抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException:无法确定合适的驱动程序类

抛出的错误描述如下,

说明:无法配置数据源:未指定url"属性,无法配置嵌入的数据源.原因:未能确定合适的驱动程序类行动:考虑以下几点:
如果您想要一个嵌入式数据库(H2、HSQL 或 Derby),请将其放在类路径中.<br>如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态).

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

我的 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><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/><!-- 从存储库中查找父级 --></父母><groupId>com.example</groupId><artifactId>SL2INDUSTRY</artifactId><version>0.0.1-SNAPSHOT</version><name>SL2INDUSTRY</name><description>Spring Boot 的演示项目</description><属性><java.version>1.8</java.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>运行时</scope></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper --><依赖><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><范围>提供</范围></依赖><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat --><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.1.3.RELEASE</version></依赖><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml --><依赖><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><version>2.9.8</version></依赖></依赖项><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build></项目>

<块引用>

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

application.properties 文件

spring.mvc.view.suffix=.jsp

解决方案

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

在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration"的bean时出错:表达了不满意的依赖通过构造函数参数0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中定义名称为dataSource"的 bean 创建错误:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.zaxxer.hikari.HikariDataSource]:工厂方法dataSource"抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException:无法确定合适的驱动程序类

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

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

正如你在这个地址看到的:

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

<块引用>

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

这是从 Spring Boot 1.x 的变化:

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

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

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

和Hikari的配置不同.

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

<依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><排除事项><排除><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></排除></排除项></依赖><依赖><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jdbc</artifactId><version>9.0.10</version></依赖>

<块引用>

这将默认从您的 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//其他 spring 数据源属性spring.datasource.url=...spring.datasource.username=...spring.datasource.password=......

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

https://www.baeldung.com/spring-boot-tomcat-连接池

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

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

但是这两个选项之一可以解决您的问题.

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.xml file. But I don't know what to change and even similar type questions on StackOverflow couldn't help me to sort this out.

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

    <?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.jackson.dataformat/jackson-dataformat-xml -->
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-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.

Edits: application.properties file

spring.mvc.view.suffix=.jsp

解决方案

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?

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

As you can see at this address:

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.

And the configuration of Hikari is diferent.

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

<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.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:

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

If you want use Hikari the configuration is diferent:

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

But one of these two options will fix your problem.

这篇关于Spring Boot:无法配置数据源:未指定“url"属性且无法配置嵌入式数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆