深入学习 Spring Web 开发 —— 依赖引入

深入学习 Spring Web 开发 —— 依赖引入

深入学习 Spring Web 开发 —— 依赖引入

[TOC]

上一篇文章介绍了如何快速搭建一个 Spring Web 项目,本文重点聊聊项目的依赖是如何引入的。

我们前面提到,搭建 Spring Web 项目时,只需要继承 spring-boot-starter-parent 并指定它的版本,接着引入 spring-boot-starter-web ,且无需指定 spring-boot-starter-web 的版本,即可把 Spring Web 项目所需要的全部依赖引进来,具体是如何做到的呢?

这里会涉及到 Maven 的 parent 和 dependencyManagement 标签,我们先讲讲这两个标签的作用。

Maven 标签

parent

在 Maven 项目中,可以通过继承的方式,让子项目继承父项目所定义的内容,如:继承 groupId、version、properties、dependencies 等。

下面的例子中,my-app-child 只需要继承 my-app-parent ,即可引入父项目的全部依赖。即父项目 my-app-parent 引入了 maven-artifact 和 maven-core 两个依赖,并指定了它们的版本。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.mycompany.app

my-app-parent

1.0-SNAPSHOT

my-app-parent

http://www.example.com

3.0

org.apache.maven

maven-artifact

${mavenVersion}

org.apache.maven

maven-core

${mavenVersion}

子项目只需继承 my-app-parent 并指定它的版本,就引入了这两个依赖,并且依赖的版本也跟父项目所指定的版本一样。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.mycompany.app

my-app-parent

1.0-SNAPSHOT

my-app-child

dependencyManagement

有时候子项目并不需要引入父项目的全部依赖,只需要引入部分依赖,但又希望在父项目中统一定义依赖的版本,dependencyManagement 标签可以帮我们完成这个事情。

下面的例子中,my-app-child 引入了 maven-core 依赖,父项目仅仅只是预定义了依赖的版本。也就是说,父项目指定了 maven-artifact 和 maven-core 两个依赖的版本,但并没有引入这两个依赖。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.mycompany.app

my-app-parent

1.0-SNAPSHOT

my-app-parent

http://www.example.com

3.0

org.apache.maven

maven-artifact

${mavenVersion}

org.apache.maven

maven-core

${mavenVersion}

在子项目中只引入了 maven-core 依赖,即 maven-artifact 是没有被引入的,且子项目无需指定 maven-core 依赖的版本,该依赖的版本就与父项目所指定的版本一样。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.mycompany.app

my-app-parent

1.0-SNAPSHOT

my-app-child

org.apache.maven

maven-core

Spring Web 依赖的引入

spring-boot-starter-parent

2.7.2 版本的 spring-boot-starter-parent 继承自父项目 spring-boot-dependencies。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

4.0.0

org.springframework.boot

spring-boot-dependencies

2.7.2

spring-boot-dependencies 通过 dependencyManagement 标签预先指定了各个 starter 和其它各个依赖的版本。尤其是,将 spring-boot-starter-web 的版本指定为 2.7.2 。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

4.0.0

org.springframework.boot

spring-boot-dependencies

2.7.2

pom

spring-boot-dependencies

org.apache.activemq

activemq-amqp

${activemq.version}

org.apache.activemq

activemq-blueprint

${activemq.version}

org.springframework.boot

spring-boot-starter-web

2.7.2

org.springframework.boot

spring-boot-starter-webflux

2.7.2

org.springframework.boot

spring-boot-starter-websocket

2.7.2

spring-boot-starter-web

2.7.2 版本的 spring-boot-starter-web 将 Spring Web 项目需要的全部依赖,如 spring-web 等引入了进来,并指定了它们的版本:

xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

4.0.0

org.springframework.boot

spring-boot-starter-web

2.7.2

spring-boot-starter-web

org.springframework.boot

spring-boot-starter

2.7.2

compile

org.springframework.boot

spring-boot-starter-json

2.7.2

compile

org.springframework.boot

spring-boot-starter-tomcat

2.7.2

compile

org.springframework

spring-web

5.3.22

compile

org.springframework

spring-webmvc

5.3.22

compile

因此,在我们继承 2.7.2 版本的 spring-boot-starter-parent ,并引入 spring-boot-starter-web 之后,就相当于引入了 2.7.2 版本的 spring-boot-starter-web ,2.7.2 版本的 spring-boot-starter-web 又将该版本所需要的特定版本的依赖引入了进来,从而 Spring Web 项目就能够在 spring-boot-starter-parent 和 spring-boot-starter-web 的共同作用下,将项目所需要的依赖和依赖版本全部定义好。并且,在这个过程中,我们不需要的依赖,如 spring-boot-starter-webflux 等,是没有被引入进来的,这样就达到了既方便又灵活的效果。

读到这里,不知道读者有没有这样的疑问:我继承的是 spring-boot-starter-parent 项目,为什么却把 spring-boot-dependencies 的内容也继承了?这其实是由继承的传递性造成的,即继承的特性导致了子项目除了会继承父项目的内容,同时也会继承所有其它祖先项目的内容。

返回首页

获取源码

paitse is maintained by susamlu.

This page was generated by GitHub Pages.

相关推荐

佐助身高
365bet体育在线赌场

佐助身高

📅 07-07 👁️ 1624
世界杯历史进球最多的四大球员盘点:梅西领衔榜单
365bet体育在线赌场

世界杯历史进球最多的四大球员盘点:梅西领衔榜单

📅 07-05 👁️ 6550
PlayStation 4
精准原创123656官方网

PlayStation 4

📅 07-12 👁️ 7146
游戏安全02:手游外挂简单分类和实现原理介绍
365bet体育在线赌场

游戏安全02:手游外挂简单分类和实现原理介绍

📅 07-06 👁️ 3161
企业为什么要上erp
精准原创123656官方网

企业为什么要上erp

📅 07-12 👁️ 5051
阴阳师神龛ssr多久一次2021 具体介绍
365游戏注册

阴阳师神龛ssr多久一次2021 具体介绍

📅 07-08 👁️ 7430
教你自己动手搭建一个传奇游戏,自己和自己玩,找找当年的感觉
正点原子STM32学习笔记——MPU6050介绍
365bet体育在线赌场

正点原子STM32学习笔记——MPU6050介绍

📅 06-28 👁️ 2025
《豪门盛宴》
精准原创123656官方网

《豪门盛宴》

📅 06-30 👁️ 4509