在创建spring boot工程时,spring-boot-starter-parent 和 spring-boot-dependencies是二选一的关系,在pom中引入其中一个就可以了。
那么什么时候用spring-boot-starter-parent 和 spring-boot-dependencies呢?从字面名称上看,如果我们要通过继承的方式引入springboot框架,那么我们使用spring-boot-starter-parent ,如果想通过依赖的方式引入springboot框架,则我们使用spring-boot-dependencies。
下面给出我们常见的作法:
我们需要在 parent 标签里引入spring-boot-starter-parent,然后在dependencies标签里选择需要的具体依赖
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>boot-testartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.12.RELEASEversion>
parent>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
我们需要在 dependencyManagement 标签里引入spring-boot-dependencies,然后在dependencies标签里选择需要的具体依赖,如下:
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>boot-testartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.3.12.RELEASEversion>
<type>pomtype>
<scope>importscope>
<optional>trueoptional>
dependency>
dependencies>
dependencyManagement>
project>
以上是我们常规的方法,其实我试了下把 spring-boot-dependencies 作为parent方式即第一种方式,项目也是可以启动成功的
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>boot-testartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.3.12.RELEASEversion>
parent>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
这种方式不是推荐的方式,因为springboot框架已经很贴心的给我们区分了两个概念;
如果需要通过parent方式的话,就是用spring-boot-starter-parent,如果需要通过依赖的方式引入则使用spring-boot-dependencies
我们点开spring-boot-starter-parent的pom内容,如下:
4.0.0
org.springframework.boot
spring-boot-dependencies
2.3.12.RELEASE
spring-boot-starter-parent
pom
spring-boot-starter-parent
Parent pom providing dependency and plugin management for applications built with Maven
1.8
@
$ {java.version}
$ {java.version}
UTF-8
UTF-8
...
很清晰的看到spring-boot-starter-parent是以spring-boot-dependencies为parent的,只是加了一些maven build的插件而已,所以两者内容虽然不能说很相似,但可以说是完全一样。
按照潜规则:想用继承方式我们就用spring-boot-starter-parent,如果想通过依赖方式引入的话就用spring-boot-dependencies
点开 spring-boot-dependencies,里面大部分是各种starter,即各种独立功能自动装配的依赖;
大家看下这个图就明白了:
starter命名规则可以看我另一篇文章:https://blog.csdn.net/Aqu415/article/details/115875840
over~~