Spring Boot Starter中Maven依赖版本如何指定?以mongodb-driver为例
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:
The
spring-boot-starter-parentmagic
When you create a standard Spring Boot project, yourpom.xmltypically inherits fromspring-boot-starter-parent. This parent POM includes a massive<dependencyManagement>section that defines exact versions for hundreds of common dependencies—includingmongodb-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.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 thespring-boot-dependenciesBOM 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.
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 ownpom.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

