springboot之核心依赖spring-boot-starter,spring-boot-starter-parent,spring-boot-starter-web依赖解析

本文详细解析了Spring Boot中各种starter的使用,包括spring-boot-starter-parent、spring-boot-starter、spring-boot-starter-web和spring-boot-starter-test,阐述了它们在项目中的作用及如何简化依赖管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://blog.csdn.net/zhou920786312/article/details/84324915

https://blog.csdn.net/tr1912/article/details/79217132

spring-boot-starter:spring-boot场景启动器,后面跟的单词就是场景,比如说后面跟web,就是导入web场景的所有依赖。

1. spring-boot-starter-parent(控制版本信息)

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.4.RELEASE</version>
   <relativePath/> 
</parent>

Spring Boot的版本仲裁中心,控制了所有依赖的版本号,

好处:以后我们导入依赖默认是不需要写版本;

2.spring-boot-starter

Spring Boot的核心启动器,包含了自动配置、日志和YAML

3. spring-boot-starter-web

web的场景,自动帮我们引入了web模块开发需要的相关jar包

4. spring-boot-starter-test

springboot程序测试依赖,如果是自动创建项目默认添加

一个正常springboot项目启动,依赖的基础包:

<dependencies>
   <!--springboot程序测试依赖,如果是自动创建项目默认添加-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
   <!--springboot web模块支持,自动帮我们引入了web模块开发需要的相关jar包-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

 

### 配置继承 `spring-boot-starter-parent` 的方法 在 Maven 构建工具中,可以通过设置项目的根 `pom.xml` 文件来继承 `spring-boot-starter-parent`。以下是具体的配置方式: #### 1. 设置父项目 在 `pom.xml` 中定义 `<parent>` 节点,指定其 groupId、artifactId version 属性。 ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <!-- 这里填写所需的 Spring Boot 版本 --> </parent> ``` 通过上述配置,当前项目会自动继承来自 `spring-boot-starter-parent` 提供的一系列默认配置依赖管理[^1]。 #### 2. 添加启动器依赖 为了使项目具备基本功能,通常还需要引入至少一个 Starter 依赖。例如,如果需要创建一个 Web 应用程序,则可以添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 由于已经继承了 `spring-boot-starter-parent`,因此无需显式声明版本号,因为这些版本会被自动管理[^2]。 #### 完整示例 下面展示了一个完整的 `pom.xml` 示例文件结构: ```xml <?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <!-- 替换为所需的具体版本 --> </parent> <groupId>com.example</groupId> <artifactId>demo-project</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 此配置不仅继承了 `spring-boot-starter-parent` 所提供的默认构建支持,还集成了必要的插件用于打包可执行 JAR 文件。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值