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

Spring Boot多模块项目子模块resources配置文件未生效问题

问题根因

配置不生效核心是3个配置错误:

  1. spring-boot-starter-parent依赖引入方式完全错误:该依赖是用来作为Maven父POM继承的,不能放在普通<dependencies>节点下引入,这种写法不会触发Spring Boot默认的类路径扫描、资源目录识别、插件绑定逻辑,Maven编译时根本不会把子模块src/main/resources下的配置文件打包进类路径,Spring Boot自然读不到配置。
  2. 父POM中手动配置的<sourceDirectory>src/main/java</sourceDirectory>覆盖了Maven默认的目录规则,但没有配套配置<resources>节点把资源目录纳入编译输出,进一步导致配置文件不会进入类路径。
  3. 子模块打包插件选型错误:maven-assembly-plugin不会按照Spring Boot可执行jar的规范组织文件结构,且你配置的主类路径main.java.com.example.projectname.search.Main是错误的,Java编译后的类路径不包含main.java前缀,启动时类加载逻辑异常。

你之前尝试的两种方案无效,本质都是因为配置文件根本没进入应用类路径,不管是配置资源路径还是加注解,都解决不了类路径缺失的根本问题。

修复方案

按以下步骤调整配置即可:

1. 修正父模块pom.xml

删除原有错误的spring-boot-starter-parent依赖引入、手动配置的sourceDirectory/testSourceDirectory节点,改为通过<parent>节点继承Spring Boot父POM,修正后核心配置如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 正确继承Spring Boot父POM -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>projectname-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>search</module>
        <module>usermanagement</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2. 修正search子模块pom.xml

删除原有maven-assembly-plugin配置,替换为Spring Boot官方的spring-boot-maven-plugin,修正主类路径,修正后核心配置如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>projectname-parent</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>search</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 使用Spring Boot官方打包插件,自动处理资源加载、jar结构规范 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.example.projectname.search.Main</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

usermanagement模块的POM配置和search模块保持一致,主类路径替换为usermanagement模块对应的启动类即可。

3. 确认目录配置

在IDE中把每个子模块的src/main/resources目录标记为Resources Root,确保自己模块的application.properties、对应环境的profile配置(比如application-dev.properties)都存放在该目录下。

4. 验证

执行mvn clean install后直接启动对应模块的Main类,不需要额外传入spring.config.additional-location参数,Spring Boot会自动加载当前模块类路径下的配置文件,spring.profiles.active=dev配置会正常生效。

补充说明
  • 不要在父模块中存放通用的application.properties,否则会被所有子模块加载,造成配置污染。如果需要公共配置,可以通过Maven资源过滤或者配置中心实现。
  • 只有作为可启动应用的子模块需要配置spring-boot-maven-plugin,如果是公共依赖模块不需要配置该插件,也不需要放置启动类。
  • 你提到的@PropertyScan注解不存在,正确的注解是@PropertySource,但对于Spring Boot默认加载的application.properties,不需要加任何额外注解,只要文件在类路径下就会被自动识别。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 02:45:49