带你认识Spring Boot Starter

一、什么是 Spring Boot Starter?

SpringBoot项目中,经常能够在pom文件中看到以spring-boot-starter-xxxx-spring-boot-starter命名的一些依赖。例如:spring-boot-starter-webspring-boot-starter-securityspring-boot-starter-data-jpamybatis-spring-boot-starter等等。

Spring Boot Starter 是一组便捷的依赖描述符,它们封装了特定技术或功能的所有必要依赖项和配置,使开发者能够快速将这些功能集成到 Spring Boot 项目中。每个 Starter 都对应一个功能或技术栈,通过引入相应的 Starter,开发者无需手动配置和管理多个相关依赖项,就能轻松启用该功能。

二、Starter 与普通依赖的区别

Spring Boot Starter 和普通依赖(普通 Jar 包)的主要区别在于 Starter 是一组便捷的依赖集合和自动配置的封装,而普通依赖只是单一的功能库或框架。以下是一些对比:

1. 依赖管理
  • Spring Boot Starter:

    • 通过一个依赖引入一组相关依赖。例如引入 spring-boot-starter-web 会同时引入 Spring MVC、Tomcat 和 Jackson 等依赖。
    • 简化了项目的依赖管理,减少了配置的复杂性。
  • 普通依赖:

    • 需要手动逐个添加相关的依赖。例如,如果需要 Spring MVC 和 Jackson,需要分别添加这两个依赖。依赖管理较为繁琐,需要开发者对各个依赖及其版本有较好的掌握。
2. 自动配置
  • Spring Boot Starter:

    • 通常配合 Spring Boot 的自动配置机制使用,提供默认的自动配置,使项目开箱即用。

    • 通过 spring.factories 文件自动注册相关的自动配置类,无需手动配置 Bean。

      新版本中,文件名为org.springframework.boot.autoconfigure.AutoConfiguration.imports

  • 普通依赖:

    • 需要手动进行相关配置。使用某个库后,需要在配置文件或代码中手动配置对应的 Bean。
    • 缺乏统一的自动配置机制,配置过程可能较为繁琐。
3. 最佳实践
  • Spring Boot Starter:

    • 封装了社区和 Spring 团队的最佳实践,提供合理的默认配置,适合大多数应用场景。
    • 减少了初学者的学习曲线,开发者无需了解底层实现细节即可快速使用。
  • 普通依赖:

    • 配置依赖时需要开发者自行决定各个依赖的配置方式,可能会导致不一致或不合理的配置。
    • 适合需要细粒度控制和定制化配置的场景。
4. 模块化
  • Spring Boot Starter:

    • 提供了功能模块化的方式,通过不同的 Starter 可以快速集成不同的功能模块。
    • 例如,使用 spring-boot-starter-data-jpa 可以快速集成 JPA 持久化,使用 spring-boot-starter-security 可以快速集成安全功能。
  • 普通依赖:

    • 需要手动组合和配置不同的依赖,缺乏模块化的便捷性。
    • 不同的依赖之间可能存在兼容性问题,需要开发者自行解决。

三、为什么需要自定义 Starter?

Spring Boot 项目中,自定义 Starter 可以极大地简化和标准化特定功能的集成和配置。以下是一些需要使用自定义 Starter 的场景:

1. 重复使用的通用功能模块

当你有多个项目需要使用相同的功能模块时,可以创建一个自定义 Starter,将这些功能模块封装起来。这样可以减少代码重复,提高代码复用性。

示例:统一的日志处理模块、统一的异常处理模块等。

2. 复杂的第三方库集成

当集成第三方库时,如果需要复杂的配置和初始化,可以通过自定义 Starter 简化集成过程,提供更友好的使用方式。

示例:集成 KafkaRedisElasticsearch 等中间件。

3. 公司内部的标准化开发框架

在企业内部,可以将一些标准化的开发规范和基础设施封装成自定义 Starter,使得团队内的所有项目都能遵循相同的规范和使用相同的基础设施。

示例:公司内部的监控系统、认证授权模块、消息队列封装等。

4. 统一配置和管理

当需要统一管理和配置某些功能时,可以通过自定义 Starter 进行集中管理,提供默认配置,并允许项目根据需要进行覆盖配置。

示例:统一的数据库连接池配置、安全配置等。

5. 封装复杂的初始化逻辑

有些功能的初始化过程非常复杂,如果每个项目都需要重复这部分初始化逻辑,可以通过自定义 Starter 封装这部分逻辑,简化项目的初始化过程。

示例:复杂的缓存管理、事务管理等。

6. 提供公共的 Bean 和配置

当需要在多个项目中提供相同的 Bean 和配置时,可以通过自定义 Starter 提供这些公共的 Bean 和配置,避免每个项目都重复定义。

示例:统一的服务发现客户端、统一的负载均衡策略等。

四、如何自定义实现 Starter?

以下是自定义 Spring Boot Starter 的详细步骤和示例:

1. 创建项目结构

创建一个新的 Maven 项目,项目结构如下:

/**
* 【微观小世界】奇趣探险,启程进入神秘的微观世界!
* https://www.sanzhiwa.top/6704.html
*/
my-custom-starter
│
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── starter
│   │   │               ├── AutoConfiguration.java
│   │   │               └── ExampleService.java
│   │   └── resources
│   │       └── META-INF
│   │           └── spring.factories
│   └── test
├── pom.xml
└── ...
2. 配置 pom.xml

在 pom.xml 中添加必要的依赖:

/**
* 儿童科普动画大放送,探索知识的奇妙世界!
* https://www.sanzhiwa.top/6700.html
*/
<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://www.mostofthemaven.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-custom-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
​
    <name>My Custom Starter</name>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3. 编写自动配置类

创建 AutoConfiguration.java 文件,实现自动配置逻辑

/**
* 清华附小小学动画
* https://www.sanzhiwa.top/6686.html
*/
package com.example.starter;
​
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
@Configuration
public class AutoConfiguration {
​
    @Bean
    public ExampleService exampleService() {
        return new ExampleService();
    }
}
4. 创建业务逻辑类

创建 ExampleService.java 文件,实现具体业务逻辑:

/**
* 央视力荐72部搞钱纪录片搞钱先搞脑
* https://www.sanzhiwa.top/6736.html
*/
package com.example.starter;
​
public class ExampleService {
​
    public String sayHello() {
        return "Hello from ExampleService!";
    }
}
5. 配置 spring.factories

在 src/main/resources/META-INF/spring.factories 文件中配置自动配置类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.starter.AutoConfiguration
6. 使用自定义 Starter

在其他 Spring Boot 项目中引入自定义 Starter:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-custom-starter</artifactId>
    <version>1.0.0</version>
</dependency>

并在项目中使用 ExampleService

/**
* 探索英语的新世界,从《新概念英语》开始!
* https://www.sanzhiwa.top/6696.html
*/
import com.example.starter.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
public class ExampleController {
​
    @Autowired
    private ExampleService exampleService;
​
    @GetMapping("/hello")
    public String hello() {
        return exampleService.sayHello();
    }
}

五、总结

自定义 Spring Boot Starter 是一种简化项目配置和提高代码复用性的重要手段。通过封装复杂的配置逻辑和依赖关系,开发者可以更专注于业务逻辑,实现更高效的开发流程。希望本文的介绍和示例能够帮助你理解和实现自定义 Spring Boot Starter

  • 11
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值