SpringBoot启动web项目的最少依赖

1、pom.xml 文件:启动web项目

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>

    <groupId>com.manmanqiu</groupId>
    <artifactId>asdasdas</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--对全栈web开发的支持,包括Tomcat和spring-webmvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.18</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、为什么:只需要web依赖?

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.7.18</version>
</dependency>

spring-boot-starter-web 依赖已经包括了启动一个 Web 项目所需的所有关键组件,包括嵌入式 Tomcat 和 Spring MVC 框架。因此,你不需要额外配置 Tomcat 服务器,它已经默认配置好并会在应用启动时运行。

  • 嵌入式 Tomcat

    spring-boot-starter-web 依赖默认包含 嵌入式 Tomcat 服务器,这是 Spring Boot 的一大特点。你不需要单独配置外部服务器,Spring Boot 会自动将 Tomcat 嵌入到应用程序中,并在启动时运行 Tomcat 作为 Web 容器来处理 HTTP 请求。

  • Spring MVC

    这个依赖包中包含了 Spring MVC 框架,它是构建 Web 应用的核心模块。
    Spring MVC 负责处理 HTTP 请求并将它们映射到适当的控制器方法。

  • Jackson

    Jackson 是处理 JSON 序列化和反序列化 的工具库,它被用来处理 REST API 中常见的 JSON 数据格式。这个库也是 spring-boot-starter-web 依赖中的一部分,允许你轻松地构建 RESTful 服务。

  • Spring Boot 自动配置

    Spring Boot 通过自动配置功能简化了项目配置。
    基于依赖的存在,Spring Boot 自动配置了一些必要的组件。
    例如,因为你引入了 spring-boot-starter-web,它会自动配置一个嵌入式 Tomcat 服务器和 Spring MVC 组件。

3、该web依赖的:依赖树

bash 复制代码
+- org.springframework.boot:spring-boot-starter-web:jar:2.7.18:compile
|  +- org.springframework.boot:spring-boot-starter:jar:2.7.18:compile
|  |  +- org.springframework.boot:spring-boot:jar:2.7.18:compile
|  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.18:compile
|  +- org.springframework.boot:spring-boot-starter-json:jar:2.7.18:compile
|  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.18:compile
|  +- org.springframework:spring-web:jar:5.3.25:compile
|  +- org.springframework:spring-webmvc:jar:5.3.25:compile
|  +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.5:compile
java 复制代码
org.springframework.boot:spring-boot-starter-web 依赖包含了 org.springframework.boot:spring-boot-starter 的原因是,Spring Boot Starter 依赖(如 spring-boot-starter-web)是通过组合多个常用依赖来简化项目配置的,这就是 Spring Boot 的"starter"概念。

spring-boot-starter 的作用
spring-boot-starter 是所有 Spring Boot Starter 依赖的基础,它包含了一些基础的配置和工具,使得 Spring Boot 项目能顺利启动并运行。它负责引入一些基础的、通用的依赖。以下是它的主要组成部分:

1. spring-boot
spring-boot 包含了启动 Spring Boot 应用的核心类,比如 SpringApplication 类。这个类的 run() 方法用于启动你的 Spring Boot 应用。

2. spring-boot-autoconfigure
spring-boot-autoconfigure 是 Spring Boot 中的自动配置机制,它基于你的类路径上的依赖,自动配置应用所需的 Bean 和组件。比如你有 spring-boot-starter-web 依赖,Spring Boot 自动为你配置了 Web 服务器(如嵌入式 Tomcat)、Spring MVC 相关的 Controller、View Resolver 等。

spring-boot-starter-web 和 spring-boot-starter 的关系
spring-boot-starter-web 这个依赖主要是为 Web 应用设计的,它不仅需要 Web 相关的依赖(如 spring-webmvc 和 spring-boot-starter-tomcat),还需要 Spring Boot 的基础配置和功能,因此它包含了 spring-boot-starter 作为依赖之一。
spring-boot-starter 则提供了所有 Spring Boot 项目都需要的通用基础组件,比如 spring-boot 和 spring-boot-autoconfigure。
总结:
org.springframework.boot:spring-boot-starter 是 Spring Boot 项目的基础依赖,spring-boot-starter-web 依赖它来确保应用有核心启动类和自动配置机制,这样你才能够顺利启动和运行 Spring Boot Web 应用。

4、spring-boot-starter 会重复引入吗?

  • 问题如下

spring-boot-starter-data-jpa spring-boot-starter-security 并没有指定spring-boot-starter 版本的情况下,

是以spring-boot-starter-data-jpa 加载时的版本为主吗? spring-boot-starter-security 跟随吗?

xml 复制代码
在你没有手动指定 spring-boot-starter 的版本时,Spring Boot 的依赖管理机制会自动确保所有 starter(如 spring-boot-starter-data-jpa 和 spring-boot-starter-security)使用相同的版本。这是通过 spring-boot-dependencies BOM(Bill of Materials)进行统一版本管理的。

BOM 的作用是什么?
BOM 是一个 Maven 特性,Spring Boot 使用它来提供一个依赖版本的统一管理,使得在你的 pom.xml 文件中不需要显式地为每个依赖指定版本。

spring-boot-starter-data-jpa 和 spring-boot-starter-security 这两个依赖,在内部会依赖于 spring-boot-starter。
当你引入这两个依赖时,它们会根据当前 Spring Boot 的版本,通过 BOM 确定每个相关依赖的版本。这意味着,两个依赖引入的 spring-boot-starter 版本是完全一致的,由 Spring Boot 版本统一决定。
示例说明
假设你没有引入 spring-boot-starter-parent,而直接引入了 spring-boot-starter-data-jpa 和 spring-boot-starter-security,它们的内部依赖树是:

spring-boot-starter-data-jpa
依赖于 spring-boot-starter
通过 BOM 确定它所使用的 spring-boot-starter 版本。
spring-boot-starter-security
同样依赖于 spring-boot-starter
版本由 BOM 决定,与 spring-boot-starter-data-jpa 保持一致。
你不需要手动指定版本,Maven 会根据 BOM 来统一决定这两个依赖中的 spring-boot-starter 使用的版本。因此,不是其中一个依赖决定版本,另一个跟随,而是它们都通过 BOM 使用相同的版本。

如何 BOM 起作用
当你引入 spring-boot-starter-parent 或者通过 spring-boot-dependencies 进行版本管理时,BOM 会自动为你定义所有相关依赖的版本。你只需要指定 Spring Boot 的版本,Maven 会为每个 Spring Boot 相关的依赖(包括 spring-boot-starter-data-jpa、spring-boot-starter-security 和其他 starter)选择对应的版本。


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.18</version> <!-- 这里统一版本 -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

spring-boot-starter-data-jpa 和 spring-boot-starter-security 引入的 spring-boot-starter 版本不依赖于任何一个特定的 starter,而是由 Spring Boot 的 BOM 决定。
版本一致性是由 Spring Boot 的依赖管理(BOM)统一决定的,确保不同 starter 使用的依赖版本一致,避免兼容性问题。
相关推荐
kyriewen113 小时前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
skywalk81635 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
wb043072015 小时前
使用 Java 开发 MCP 服务并发布到 Maven 中央仓库完整指南
java·开发语言·spring boot·ai·maven
nbwenren6 小时前
Springboot中SLF4J详解
java·spring boot·后端
RopenYuan7 小时前
FastAPI -API Router的应用
前端·网络·python
helx827 小时前
SpringBoot中自定义Starter
java·spring boot·后端
走粥7 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory0018 小时前
layui select重新渲染
前端·layui
rleS IONS8 小时前
SpringBoot获取bean的几种方式
java·spring boot·后端