- 内容简介
- 前言
- 第 1 章 第一个 Spring Boot 项目
- 第 2 章 集成 MySQL 数据库
- 第 3 章 集成 Spring Data JPA
- 第 4 章 使用 Thymeleaf 模板引擎
- 第 5 章 Spring Boot 事务支持
- 第 6 章 使用过滤器和监听器
- 第 7 章 集成 Redis 缓存
- 第 8 章 集成 Log4j 日志
- 8.3 使用 Log4j 记录日志
- 第 9 章 Quartz 定时器和发送 Email
- 第 10 章 集成 MyBatis
- 第 11 章 异步消息与异步调用
- 第 12 章 全局异常处理与 Retry 重试
- 第 13 章 集成 MongoDB 数据库
- 第 14 章 集成 Spring Security
- 第 15 章 Spring Boot 应用监控
- 第 16 章 集成 Dubbo 和 Zookeeper
- 第 17 章 多环境配置与部署
- 第 18 章 Spring Boot 原理解析
- 参考文献
18.3 spring-boot-starter 原理

在之前的章节中,我们在项目的 pom 文件中引入了很多 spring-boot-starter 依赖,比如 spring-boot-starter-jdbc、spring-boot-starter-jdbc-logging、spring-boot-starter-web 等。这些带有 spring-boot-starter 前缀的依赖都叫作 Spring Boot 起步依赖,它们有助于 Spring Boot 应用程序的构建。
如果没有起步依赖,要使用 Spring MVC 的话,我们根本记不住 Spring MVC 到底要引入哪些依赖包、到底要使用哪个版本的 Spring MVC、Spring MVC 的 Group 和 Artifact ID 又是多少。
Spring Boot 通过提供众多起步依赖降低项目依赖的复杂度。起步依赖本质上是一个 Maven 项目对象模型,定义了对其他库的传递依赖,这些依赖的合集可以对外提供某项功能。起步依赖的命名表明它们提供某种或某类功能。例如,spring-boot-starter-jdbc 表示提供 jdbc 相关的功能,spring-boot-starter-jpa 表示提供 JPA 相关的功能,等等。下面简单列举工作中经常使用的起步依赖,如表 18-1 所示。
表 18-1 常用的 spring-boot-starter 起步依赖
事实上,起步依赖和项目里的其他依赖没什么区别,引入起步依赖的同时会引入相关的传递依赖。例如,spring-boot-starter-web 起步依赖会引入 spring-webmvc、jackson-databind、spring-boot-starter-tomcat 等传递依赖。如果我们不想用 spring-boot-starter-web 引入的 spring-webmvc 传递依赖,那么可以使用<exclusions>标签来排除传递依赖。具体代码如下:
假如 spring-boot-starter-web 引入的传递依赖版本过低,我们可以在 pom 文件中直接引入所需的版本,告诉 Maven 现在需要这个版本的依赖。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论