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

SpringBoot 3.x无法加载src/main/resources下的application.properties求助

解决SpringBoot 3.x未加载application.properties的问题

核心问题分析

你的问题本质是SpringBoot未正确读取到目标配置文件, fallback使用了默认数据源配置,大概率是配置路径指定错误、Profile激活逻辑冲突,或是配置文件本身存在语法问题。

具体解决步骤

1. 修正配置文件路径参数

你当前使用的spring.config.location参数写法有误,必须指定正确的类路径标识:

  • VM选项需改为:-Dspring.config.location=classpath:/application.properties
  • 程序参数需改为:--spring.config.location=classpath:/application.properties
    注意:classpath:/前缀不可省略,否则Spring会去文件系统根目录查找,而非项目类路径下的target/classes。

2. 简化Profile激活逻辑

既然你已在application.properties中设置spring.profile.active=local,无需在运行参数中重复指定。若要激活local环境,更规范的方式是:

  • VM选项:-Dspring.profiles.active=local
  • 程序参数:--spring.profiles.active=local
    Spring会自动加载application-local.properties(若存在),同时优先读取主配置文件的基础配置。

3. 检查配置文件语法

确认application.properties中的数据源配置无语法错误,示例正确配置如下:

# 数据源配置示例
spring.datasource.url=jdbc:mysql://localhost:3306/your_db_name
spring.datasource.username=your_db_user
spring.datasource.password=your_db_pwd
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

若存在键名拼写错误、缺少等号等问题,Spring会忽略该配置,自动使用默认值。

4. 验证配置加载优先级

SpringBoot 3.x配置加载优先级为:命令行参数 > VM系统属性 > 类路径下的application.properties。建议先移除所有自定义运行参数,直接启动应用,验证是否能正常读取src/main/resources下的配置文件。

5. 检查Maven打包配置

确认pom.xml未排除src/main/resources下的文件,若存在如下错误配置需删除:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>application.properties</exclude>
        </excludes>
    </resource>
</resources>

修改后执行mvn clean package重新打包。

额外排查技巧

  • 启动时添加日志参数:-Dlogging.level.org.springframework.context=DEBUG,查看Spring加载配置文件的日志,确认是否读取到目标文件。
  • 检查IDE运行配置,是否误勾选了「Override parameters」等覆盖配置的选项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 10:31:01