我的 application.properties 文件中有以下配置
#database-configurations
spring.datasource.url=jdbc:mysql://localhost:3306/exam
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class=com.mysql.cj.jdbc.Driver
#jpa-configurations
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
但是应用程序无法使用这些配置启动。之前工作得很好。恰巧我误删了application.properties文件中的配置并启动了spring-boot应用程序。启动失败,显然是application.properties中没有配置。该错误表明无法找到数据源 URL。但现在即使我在 application.properties 文件中正确输入所有详细信息,我仍然遇到相同的错误。它似乎没有从 application.properties 文件中读取值。它被我从 application.properties 中删除的配置设置卡住了。
我收到以下错误
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
[2m2023-01-22T14:55:32.603+05:30[0;39m [31mERROR[0;39m [35m152247[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m:[0;39m
***************************
APPLICATION FAILED TO START
***************************
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:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Spring Tool Suit 在“additional-spring-configuration-metadata”文件的 META-INF 文件夹中添加了以下几行。
{"properties": [{
"name": "spring.datasource.driver-class",
"type": "java.lang.String",
"description": "A description for 'spring.datasource.driver-class'"
}]}
以下是我的pom.xml
<?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 https://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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.exam</groupId>
<artifactId>examserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>examserver</name>
<description>backend code for exam portal</description>
<properties>
<java.version>17</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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
启动 ApplicationContext 时出错。要显示条件评估报告,请在启用“调试”的情况下重新运行您的应用程序。 [2m2024-12-15T19:37:36.218+05:30[0;39m [31mERROR[0;39m [35m9864[0;39m [2m--- [Spring_Boot_Rest_API_Project]] [ restartedMain] [0;39m[36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m:[0;39m
application.properties 文件必须位于 src/main/resources 文件夹中。