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

Spring Boot 3.0多模块项目无法读取外部项目配置文件问题

多模块项目中读取外部依赖项目配置文件的解决方案

针对你遇到的data-core-project中data-core-application.yaml配置文件无法被多模块项目识别的问题,可通过以下几种方式解决:

1. 确保配置文件被正确打包到依赖jar中

在data-core-project的构建配置里,确认资源目录的配置规则,保证data-core-application.yaml被包含在最终打包的jar文件中:

  • Maven 配置:在项目pom.xml的<build>节点下添加资源声明
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yaml</include>
                </includes>
            </resource>
        </resources>
    </build>
    
  • Gradle 配置:在build.gradle中配置资源包含规则
    sourceSets {
        main {
            resources {
                include '**/*.yaml'
            }
        }
    }
    

2. 在依赖模块中显式加载配置文件

在auth-service-data模块的配置类上,使用@PropertySource注解指定加载外部YAML配置。由于Spring默认不支持直接解析YAML,需要自定义YamlPropertySourceFactory:

@Configuration
@PropertySource(value = "classpath:data-core-application.yaml", factory = YamlPropertySourceFactory.class)
public class DataCoreConfig {
    // 配置类逻辑
}

自定义解析工厂实现:

public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        Properties properties = factory.getObject();
        return new PropertiesPropertySource(
            name != null ? name : resource.getResource().getFilename(), 
            properties
        );
    }
}

3. 使用Spring Boot配置导入功能

在auth-service-app模块的主配置文件(如application.yaml)中,通过spring.config.import直接导入外部配置文件:

spring:
  config:
    import: classpath:data-core-application.yaml

此方式无需额外编写配置类,Spring Boot会自动识别并加载指定的配置文件。

4. 校验依赖传递的正确性

确认auth-service-data模块对data-core-project的依赖声明正确,执行本地构建命令(mvn clean install或gradle clean build),确保data-core-project的jar包已正确安装到本地仓库,且auth-service-data能正常依赖到包含配置文件的jar包。

内容的提问来源于stack exchange,提问作者Cihan gürkan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 10:50:55