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

多模块Spring项目中如何配置backend.properties以连接MongoDB

Fixing Ignored backend.properties in Multi-Module Spring Boot Project

Hey there! Let's fix that ignored backend.properties issue in your multi-module Spring Boot project. Here's a step-by-step breakdown to get your MongoDB connection working:

1. Ensure backend.properties is Packaged into the Classpath

First, we need to make sure Maven includes your backend.properties file in the final JAR/WAR of the sample-webservice module (since that's the one with the main class).

Add this configuration to the pom.xml of your sample-webservice module:

<build>
    <resources>
        <resource>
            <!-- Adjust this directory if your file isn't in src/main/resources -->
            <directory>src/main/resources</directory>
            <includes>
                <include>backend.properties</include>
                <!-- Keep this if you use application.properties files too -->
                <include>application*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

If your backend.properties is stored in a different location (like a shared config folder in the parent project), update the <directory> path to point to it (e.g., <directory>../config</directory> for a parent-level config folder).

2. Tell Spring Boot to Load backend.properties

Next, you need to instruct Spring Boot to read this custom properties file. You have two simple options:

Option A: Use @PropertySource on Your Main Class

Add the @PropertySource annotation to your main application class in sample-webservice:

@SpringBootApplication
@PropertySource("classpath:backend.properties")
public class SampleWebserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleWebserviceApplication.class, args);
    }
}

If you need to load multiple properties files, use @PropertySources to list them all.

Option B: Import via application.properties

If you already have an application.properties file in sample-webservice, add this line to it to import your backend config:

spring.config.import=classpath:backend.properties

This is a flexible approach if you work with multiple configuration files or environment-specific setups.

3. Verify the Configuration is Loaded

To confirm everything works, you can add a quick test endpoint to check if the MongoDB config is being read:

@RestController
public class ConfigTestController {
    @Value("${spring.data.mongodb.database}")
    private String mongoDatabaseName;

    @GetMapping("/test-mongo-config")
    public String getMongoDbName() {
        return "Loaded MongoDB Database: " + mongoDatabaseName;
    }
}

Start your application and visit http://localhost:8080/sample/test-mongo-config. If you see "Loaded MongoDB Database: sample", your config is working correctly!

Quick Notes

  • If your parent pom.xml has a <resources> configuration, make sure it doesn't override the child module's settings (you can use <resource> inheritance or adjust includes/excludes as needed).
  • Double-check the file path in @PropertySource or spring.config.import—typos here are a common culprit!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:43:51