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

Spring Boot 3.x.x启动报错:找不到classpath下的配置文件

解决Spring Boot 3.x在STS中启动时无法加载依赖模块配置文件的问题

问题描述

在Spring Boot 3.0.6(Java 17)项目中,通过@PropertySource注解加载依赖模块src/main/resources目录下的application-lib.properties配置文件时,STS 4.x启动应用会抛出FileNotFoundException,但生成Jar包后通过命令行运行无异常。

错误日志

Caused by: java.io.FileNotFoundException: class path resource [application-lib.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:211) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:160) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:100) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:74) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:60) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.ResourcePropertySource.(ResourcePropertySource.java:67) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37) ~[spring-core-6.0.8.jar:6.0.8]
at org.springframework.core.io.support.PropertySourceProcessor.processPropertySource(PropertySourceProcessor.java:87) ~[spring-core-6.0.8.jar:6.0.8]

配置类代码

@Configuration
@PropertySource("classpath:application-lib.properties")
@EnableEncryptableProperties
public class AppConfig {

解决方案

1. 确认依赖模块的资源打包配置

如果使用Maven,检查依赖模块的pom.xml是否正确包含资源目录:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

执行依赖模块的mvn clean install,确保application-lib.properties被打包到该模块的Jar文件中。

2. 刷新STS项目并清理缓存

  • 右键主项目 → Maven → Update Project...,勾选"Force Update of Snapshots/Releases"后确认。
  • 依次执行Maven clean和Maven install,重新构建项目。
  • 重启STS后再启动应用,避免IDE缓存导致资源未加载。

3. 修改@PropertySource的路径前缀

将classpath:替换为classpath*:,扫描所有类路径(包括依赖Jar)中的匹配文件:

@PropertySource("classpath*:application-lib.properties")

classpath*:会遍历所有类路径下的资源,而classpath:仅搜索当前项目的类路径。

4. 检查STS运行配置的类路径

  • 打开STS的Run Configurations,找到对应的Spring Boot应用配置。
  • 切换到Classpath标签页,确认依赖模块的Jar或编译后的target/classes目录已被包含在类路径中。

5. 使用Spring Boot推荐的配置导入方式

替代@PropertySource,在主项目的application.properties中添加配置导入:

spring.config.import=classpath:application-lib.properties

这种方式更贴合Spring Boot的自动配置逻辑,能避免类路径扫描的兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:27:25