当我运行我的网络应用程序时抛出以下错误。
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
原文由 INDRAJITH EKANAYAKE 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您阅读了您的错误痕迹:
你会注意到他正在尝试使用 hikari 数据源来配置与 bd 的连接。这是怎么回事?
您使用的是 spring boot 2。并且此版本的 spring boot 更改了默认配置。
正如你在这个地址看到的:
https://www.baeldung.com/spring-boot-hikari#spring-boot-2
Hikari 的配置不同。
然后,我想你想使用 tomcat 连接池。你可以这样做:
这里有禁用 Hikari 和配置 tomcat 的完整参考:
https://www.baeldung.com/spring-boot-tomcat-connection-pool
如果你想使用 Hikari,配置是不同的:
https://www.baeldung.com/spring-boot-hikari
但是这两个选项之一将解决您的问题。