You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改application.properties后Spring Boot应用启动失败求助

问题详情

之前应用运行正常,误删application.properties中的配置后启动失败,提示找不到数据源URL。恢复正确配置后,仍出现相同错误,应用似乎未读取application.properties中的配置。

现有配置文件

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

报错信息

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

additional-spring-configuration-metadata.json

{"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>
排查与解决方案
  • 检查配置文件位置:确认application.properties是否放在src/main/resources目录下,这是Spring Boot默认加载配置的路径,位置错误会导致应用无法读取配置。
  • 清理缓存并重新构建:
    1. 执行Maven命令:mvn clean install,清除旧构建文件并重新打包。
    2. 在Spring Tool Suite中,右键项目→Maven→Clean,再选择Maven→Install,之后重启应用。
    3. 清除IDE缓存:执行Project→Clean...,或直接重启STS。
  • 修正配置键名:Spring Boot 3.x中数据源驱动的标准配置键为spring.datasource.driver-class-name,将配置中的spring.datasource.driver-class改为该标准键,避免键名不兼容导致配置无法识别。
  • 排查配置覆盖:检查是否存在系统环境变量、命令行参数、application.yml等其他配置文件覆盖了数据库配置,这些配置的优先级高于application.properties,会导致读取不到正确的URL。
  • 验证配置加载状态:添加配置检查类,打印加载的数据库配置,确认配置是否被正确读取:
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class ConfigValidator {
    private final Environment environment;

    public ConfigValidator(Environment environment) {
        this.environment = environment;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void printDbConfigs() {
        System.out.println("Loaded datasource URL: " + environment.getProperty("spring.datasource.url"));
        System.out.println("Loaded datasource username: " + environment.getProperty("spring.datasource.username"));
    }
}

启动应用后查看控制台输出,若值为null,说明配置未被加载,需进一步检查文件路径和构建过程。

  • 确认MySQL驱动依赖:可尝试移除mysql-connector-j的scope=runtime,或显式指定兼容版本(如8.0.32),确保驱动能被正确加载。

内容的提问来源于stack exchange,提问作者Mohit Tomar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:50:37