整合SpringBoot方式
基于Maven,整合SpringBoot有2种方式:
方式1:继承父级POM
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> |
方式2:自己编写POM
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
这里,我选择方式1,官方参考资料:
https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/#using-import
https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/#using
https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/gradle-plugin/reference/html/#getting-started
spring-boot-starter-parent
通过查看spring-boot-starter-parent-x.x.x.RELEASE.pom文件,我们可以发现其通过<parent/>标签继承自spring-boot-dependencies;
那我们继续打开spring-boot-dependencies-x.x.x.RELEASE.pom文件,然后我们发现该POM文件通过<dependencyManagement/>来声明了大量的<dependencies/>,例如:spring-boot-starter-XXXX;
结合我们在Maven3中总结的<dependencyManagement/>标签功能,该标签只是对jar的version进行管控的管理器,自身并不下载对应的jar包,因为为了使用SpringBoot,开发人员在自己项目中还是要使用<dependencies/>来声明依赖的jar包;
也就是说dependencies.pom中通过<dependencyManagement/>声明了大量的依赖包,当开发者需要某些依赖包时,需要开发者自己再在项目POM文件中通过<dependencies/>去导入,例如:
当我们需要SpringBoot核心启动器时,需要声明spring-boot-starter;
当我们需要web场景时,需要声明spring-boot-starter-web;
当我们需要测试时,需要声明spring-boot-starter-test;
SpringBoot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器;
Pom.xml代码
<?xml version="1.0" encoding="UTF-8"?> |
注意事项:<parent/>必须确保镜像Maven中能找到才行,否则一切都是徒劳;