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

Spring Boot Starter中Maven依赖版本如何指定?以mongodb-driver为例

How Spring Boot Determines Dependency Versions for Starters

Great question! Let me break down exactly how this works—it's one of Spring Boot's most convenient (but often confusing) features.

The short answer is: Spring Boot manages all these dependency versions for you via its parent POM or dependency management BOM, so you don't need to specify <version> tags in your starter dependencies.

Here's the detailed breakdown:

  1. The spring-boot-starter-parent magic
    When you create a standard Spring Boot project, your pom.xml typically inherits from spring-boot-starter-parent. This parent POM includes a massive <dependencyManagement> section that defines exact versions for hundreds of common dependencies—including mongodb-driver, spring-data-mongodb, and all other libraries used by starters.

    Think of it as a centralized version control system for your project's dependencies. Maven automatically pulls the version number from this parent configuration whenever you declare a dependency without a <version> tag.

  2. No parent POM? Use the BOM instead
    If you can't use the parent POM (like in some multi-module projects), you can still get the same version management by importing the spring-boot-dependencies BOM into your project's <dependencyManagement>:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>your-spring-boot-version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    This achieves the exact same result as using the parent POM—version numbers are centralized and automatically applied.

  3. Overriding versions when you need to
    If you ever want to use a different version of a dependency than what Spring Boot provides, just explicitly add the <version> tag to that dependency in your own pom.xml. Maven will prioritize your declared version over the one in the parent POM or BOM.

For your MongoDB Starter example:

The spring-boot-starter-data-mongodb you referenced doesn't include version tags because it relies on Spring Boot's dependency management to supply those versions. When you add this starter to your project, Maven looks up the pre-defined versions of mongodb-driver and spring-data-mongodb from the parent POM (or BOM) and uses those automatically.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:03:28