SpringBoot实战课堂笔记Spring Boot 核心之(起步依赖+自动配置)

本文使用Spring Boot 版本:org.springframework.boot:spring-boot-autoconfigure:2.3.1RELEASE

起步依赖

起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其它库的传递依赖,通过这些依赖的组合支持某项功能。这个在起步依赖的命名上是有所体现的。
在Spring Boot项目的源码中我们可以看到spring-boot-starters的子项目
在这里插入图片描述
打开其中的任意一个子项目,只有一个build.gradle文件:

plugins {
	id "org.springframework.boot.starter"
}

description = "Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container"

dependencies {
	api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
	api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-json"))
	api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
	api("org.springframework:spring-web")
	api("org.springframework:spring-webmvc")
}

在这里能看到起步依赖相关的传递依赖。

1. 指定基于功能的依赖

起步依赖的版本是由正在使用的Spring Boot的版本来决定的,所以我们不需要指定起步依赖的版本,而起步依赖会决定它们引入的传递依赖的版本。
可以在构建工具中找到所用依赖的版本,在IDEA工具中能看到类似下图的依赖树:
在这里插入图片描述

2. 覆盖起步依赖引入的传递依赖

可以通过构建工具中的功能,排除传递依赖,也可以为起步依赖没有涵盖的库指定依赖

在Gradle中排除传递依赖,build.gradle文件:

implementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}

覆盖起步依赖引入的传递依赖的版本号,这里需要注意的是Grad了更倾向于使用最新的版本,如果指明的依赖版本高于起步依赖引入的传递依赖的版本号,则不需要排除起步依赖引入的传递依赖,反之,则需要排除起步依赖引入的传递依赖

在Gradle中覆盖起步依赖,build.gradle文件:

implementation('org.springframework.boot:spring-boot-starter-test') {
	exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation('org.junit.vintage:junit-vintag
  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值