Spring 引导:无法配置数据源:未指定'url'属性,并且无法配置任何嵌入数据源



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

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

抛出错误的描述如下,

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

参考这个答案后,我知道我必须对我的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>
<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>

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

编辑:application.properties文件

spring.mvc.view.suffix=.jsp

如果您读取了错误跟踪:

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

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

您使用弹簧启动 2。并且通过这个版本的弹簧引导已经改变了默认配置。

正如您在此地址看到的:

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,我们无事可做。

光的配置是不同的。

然后,我想你想使用雄猫连接池。你可以做:

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

这将默认从您的春季 jpa 配置中排除 Hikari,然后您将需要 tomcat-jdbc。你可以配置 de 数据源:

这种简单的方法允许我们使用 Tomcat 连接池获取 Spring 引导,而无需编写@Configuration类并以编程方式定义数据源 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=...
...

在这里,您有禁用Hikari和配置雄猫的完整参考:

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

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

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

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

有时事情可能很有趣。

我已经检查了很多解决方案,包括baeldung。 和堆栈溢出

我的错误是,错误的软件包,我将配置类保留在主组件扫描包之外,我正确放置了它,它就像一个魅力。

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

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

2 - 您需要在pom上添加数据库驱动程序连接器.xml

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

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

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

如果有人在创建"project.jar"文件
后遇到此问题
当项目在 IDE/STS(弹簧工具套装)中运行时,一切都很好。
以下是该怎么做:

"application.yml"文件中不必要的空格" "可能会导致此问题。

server:
port: 8085

spring:
datasource:
url: jdbc:mysql://localhost:3306/studentdb
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
application:
name: STUDENT-SERVICE

我没有调整我的"application.yml"文件,而是简单地将"application.yml"文件

中的所有语句移动到"application.properties"文件中,并按照".properties"中要求的格式格式化语句。

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true
spring.application.name=student-service
server.port=8085

(您可以在 url 末尾添加参数)
(spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false)

选择项目 - rus as - 运行配置 - 类路径 - 用户条目 - 选择您的资源文件夹。此错误是因为您的支持人员无法读取您的资源属性文件。

最新更新