十六:Spring Boot (1)-- spring-boot-starter 应用

1. 简介:

  • spring-boot-starter 是 Spring Boot 项目中的基础启动器依赖,它为开发者提供了 Spring Boot 应用所需的核心功能和自动配置

  • spring-boot-starter 不是一个具体的功能模块,而是一个基础的启动器。

  • Spring Boot 提供了一系列的 starter 依赖,这些 starter 本质上是将相关的依赖打包成一个集合,使得开发者无需关注具体的依赖版本

2. 引入:

  • 在创建springBoot项目时,自动就引用了

  • maven:pom.xml
    *

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

3. 的工作原理:

  • 在 Spring Boot 中,starter 主要是为了简化依赖管理,特别是针对一些常见的功能(如 Web 开发、数据访问、消息队列等)。

  • 例如,spring-boot-starter-web 会自动引入 Spring MVC、Tomcat 等 Web 相关的依赖,spring-boot-starter-data-jpa 会自动引入 Spring Data JPA 和 Hibernate 相关的依赖。

  • 对于 spring-boot-starter,它主要包括一些基础的功能模块,帮助你启动和运行一个 Spring Boot 应用。你通常不会单独使用它,而是会将其与其他更具体的功能 starter 结合使用。

4.spring-boot-starter 的核心功能:

  • 自动配置 :Spring Boot 的核心理念之一是自动配置。spring-boot-starter 会根据你的类路径和配置,自动提供合适的默认配置。例如,如果你的类路径中包含 spring-boot-starter-web,Spring Boot 会自动配置 Spring MVC 和 Tomcat,而无需手动配置 DispatcherServletEmbeddedServletContainer 等。

  • 嵌入式 Web 服务器支持spring-boot-starter 包含了嵌入式 Web 服务器(如 Tomcat、Jetty、Undertow),并且这些 Web 服务器会自动被集成到 Spring Boot 应用中。你不需要独立安装和配置服务器,它会作为应用的一部分直接启动。

  • 日志配置:Spring Boot 使用 Logback 作为默认日志框架,并会自动配置应用的日志级别、格式等。如果你没有配置日志,Spring Boot 会提供一个合理的默认配置。

  • 默认配置文件加载 :Spring Boot 自动加载位于 src/main/resources 下的配置文件,如 application.propertiesapplication.yml。这些配置文件可以用来配置应用的各种参数,如端口号、数据库连接、日志设置等。

  • 集成 Spring Boot DevTools (如果启用了相应的 starter):如果你引入了 spring-boot-devtools 作为依赖,Spring Boot 将提供热部署、自动重启、改进的调试信息等功能,从而提高开发效率。

5. 如何使用 spring-boot-starter:

  • 5.1 ​​​​​​​你在 pom.xml 中引入 spring-boot-starter 后,它会自动提供一系列的功能。如果你需要使用其他特定的功能(如 Web 开发、数据访问、消息队列等),你可以将对应的 starter 添加到你的项目中。

  • 5.2 引入 spring-boot-starter-web (构建 Web 应用):

    复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
  • 5.3 引入 spring-boot-starter-data-jpa (用于 JPA 数据库交互):

    复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
  • 5.4 引入 spring-boot-starter-thymeleaf (用于 Thymeleaf 模板引擎):

    复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
  • 5.5 这些 starter 会自动为你配置相关的框架和组件,而你只需要编写业务代码。Spring Boot 的自动配置机制确保了你不用关注底层框架的细节。

相关推荐
leobertlan2 小时前
2025年终总结
前端·后端·程序员
面向Google编程2 小时前
从零学习Kafka:数据存储
后端·kafka
易安说AI3 小时前
Claude Opus 4.6 凌晨发布,我体验了一整晚,说说真实感受。
后端
易安说AI3 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
易安说AI3 小时前
用 Claude Code 远程分析生产日志,追踪 Claude Max 账户被封原因
后端
JH30734 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
颜酱4 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
Coder_Boy_5 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble5 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟5 小时前
使用ASM和agent监控属性变化
java