四篇文章带你玩转springboot——4starter组件

第1章 starter组件简介

starter组件是SpringBoot的一个核心特性,Starter组件的出现极大简化了项目开发,例如在项目中使用的pom.xm文件下配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

SpringBoot就会自动关联web开发相关的依赖,如tomcat以及spring-webmvc等,进而对web开发进行支持,同时相关技术也将实现自动配置,避免了繁琐的配置文件。

Starter组件使开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由SpringBoot自动完成class类发现并加载需要的Bean。

利用starter实现自动化配置需要两个条件:Maven依赖、配置文件,Maven依赖实质上就是导入jar包,SpringBoot启动的时候会找到Starter组件jar包中的resources/META-INF/spring.factories文件,根据spring.factories文件中的配置,找到需要自动配置的类。

starter组件理解总结:

  1. 每个不同的starter组件实际上完成自身的功能逻辑,然后对外提供一个bean对象让别人调用

  2. 对外提供的bean通过自动装配原理注入到提供方的IoC容器中  

第2章 手写starter组件

要实现一个自己的starter组件其实也很简单,要完成一个starter组件的编写,首先要明确,我们要做的事有哪些:

  1. 通过配置类提供对外服务的bean对象

  2. 按照自动装配原理完成spring.factories的编写

  3. starter自动属性配置

接下来我们就来手写一个starter组件,流程如下:

  1. 创建一个springboot项目:redisson-spring-boot-starter

  2. 引入依赖

    <?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.gupao.redisson</groupId>
      <artifactId>redisson-spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
    ​
      <name>redisson-spring-boot-starter</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    ​
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    ​
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>2.3.2.RELEASE</version>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.redisson</groupId>
          <artifactId>redisson</artifactId>
          <version>3.16.1</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <version>2.3.2.RELEASE</version>
        </dependency>
    ​
      </dependencies>
    ​
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --&g
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Spring Boot常用的Starter组件包括: 1. spring-boot-starter-web:用于构建Web应用程序的Starter组件,包括Spring MVC和Tomcat。 2. spring-boot-starter-data-jpa:用于使用JPA和Hibernate进行数据持久化的Starter组件。 3. spring-boot-starter-data-redis:用于使用Redis进行数据缓存和存储的Starter组件。 4. spring-boot-starter-security:用于构建安全Web应用程序的Starter组件,包括Spring Security和OAuth2。 5. spring-boot-starter-test:用于编写单元测试和集成测试的Starter组件,包括JUnit、Mockito和Hamcrest等。 6. spring-boot-starter-actuator:用于监控和管理Spring Boot应用程序的Starter组件,包括健康检查、度量指标和追踪等。 7. spring-boot-starter-log4j2:用于使用Log4j2进行日志记录的Starter组件。 8. spring-boot-starter-thymeleaf:用于使用Thymeleaf进行Web页面渲染的Starter组件。 9. spring-boot-starter-mail:用于使用JavaMail发送电子邮件的Starter组件。 10. spring-boot-starter-redis:用于使用Redis进行数据缓存和存储的Starter组件。 ### 回答2: Spring Boot是一个开发Spring应用程序的框架,其中包含许多常用的starter组件,这些组件可以方便地集成到项目中。 1. Web Starter:用于构建基于Web的应用程序,包括Spring MVC、Tomcat等组件,可以轻松地创建RESTful API或Web应用。 2. JPA Starter:提供了对Java持久化API的支持,可以与各种关系型数据库(如MySQL,PostgreSQL,Oracle等)进行集成,简化了数据库操作的配置和使用。 3. Security Starter:提供了基于Spring Security的安全功能,可以用于身份验证、授权和安全访问控制等。 4. Actuator Starter:用于监控和管理Spring Boot应用程序的组件,可以轻松地暴露应用程序的运行时指标和信息,如健康检查、日志、堆栈跟踪等,方便了应用程序的管理和维护。 5. Cache Starter:用于集成缓存机制的组件,可以轻松地使用缓存来提高应用程序的性能和响应时间,支持各种缓存技术,如Ehcache、Redis等。 6. Test Starter:用于编写和运行单元测试和集成测试的组件,可以方便地进行测试驱动的开发和自动化测试。 这些只是Spring Boot的一部分常用starter组件Spring Boot还提供了许多其他的starter组件,如邮件发送、消息队列、数据缓存等,这些组件可以减少开发人员的工作量,提高开发效率。 ### 回答3: Spring Boot是一个开箱即用的Java框架,它通过提供各种starter组件来简化开发过程,并提供一种快速构建可部署的独立应用的方式。下面是一些常用的Spring Boot starter组件。 1. Spring Boot Starter Web:用于构建Web应用程序的starter组件,包含了Spring MVC、Tomcat、Jackson等常用的Web开发库。 2. Spring Boot Starter Data JPA:用于操作数据库的starter组件,包含了Spring Data JPA、Hibernate等ORM框架,可以快速方便地与数据库交互。 3. Spring Boot Starter Security:提供了基于Spring Security的安全性和权限控制功能的starter组件,可以轻松地实现用户认证、授权和资源保护等功能。 4. Spring Boot Starter Test:用于编写单元测试和集成测试的starter组件,包含了常用的测试框架如JUnit、Mockito等,可以简化测试代码的编写。 5. Spring Boot Starter Actuator:提供了监控和管理Spring Boot应用程序的starter组件,可以通过暴露一些管理端点来获取应用的各种信息,如健康状况、性能指标等。 6. Spring Boot Starter Log4j2:用于日志记录的starter组件,集成了Log4j2日志框架,可以方便地配置和输出应用程序的日志信息。 7. Spring Boot Starter Mail:用于发送电子邮件的starter组件,集成了JavaMail和Spring的邮件模块,可以快速方便地发送邮件。 这些只是一些常用的Spring Boot starter组件Spring Boot还有很多其他的starter组件,可以根据具体需求选择适合的组件来简化开发过程。这些starter组件的存在大大减少了开发者的工作量,提高了开发效率,使得使用Spring Boot更加方便和易于上手。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木木_2024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值