当前位置: 首页 > news >正文

如何编写一个Spring Boot Starter

文章目录

    • 概要
    • 创建一个 Spring Boot Starter 的步骤
      • 创建一个 Maven 项目
      • 打包并发布
    • 在其他项目中使用
    • 总结

概要

写一个 Spring Boot Starter 其实就是封装一部分功能,方便其他项目引入和使用。Starter 是 Spring Boot 提供的一种机制,目的是为了简化配置模块化开发。一般来说,Spring Boot Starter 会封装一个特定的功能模块,并自动配置一些常用的组件。通过写一个 Starter,用户只需要简单地引入一个依赖,就可以使用该功能。

创建一个 Spring Boot Starter 的步骤

创建一个 Maven 项目

my-spring-boot-starter
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── hac
│   │   │           ├── config
│   │   │           │   └── HelloServiceAutoConfiguration.java
│   │   │           └── service
│   │   │               └── HelloService.java
│   │   └── resources
│   │       └── META-INF
│   │           └── spring.factories
│   └── test
└── pom.xml

代码:

public class HelloService {
    public String sayHello() {
        return "Hello, World!";
    }
}

@Configuration
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }
}

src/main/resources/META-INF/spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hac.config.HelloServiceAutoConfiguration

pom.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 https://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>2.7.18</version>
    </parent>

    <packaging>jar</packaging>

    <groupId>com.hac</groupId>
    <artifactId>hello-service-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

打包并发布

在这里插入图片描述

mvn clean install

说明:mvn install 是 Maven 中用于编译、测试、打包并将构件安装到本地仓库,便于项目管理和依赖共享。

在其他项目中使用

一、引入依赖

<dependency>
    <groupId>com.hac</groupId>
    <artifactId>hello-service-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

二、测试

@Controller
public class CartoonController {
    @Resource
    public HelloService helloService;

    @PostConstruct
    public void init() {
        System.out.println(helloService.sayHello()); // 能中共南昌打印出来
    }
}

总结

这只是一个比较简单的示例。根据具体场景实现丰富的 starter 。

注意:

  1. 命名规范:Starter 通常命名为 xxx-spring-boot-starter,自动配置类为 xxxAutoConfiguration。

  2. 版本兼容性:确保与目标项目的 Spring Boot 版本一致。

  3. 发布:考虑将 Starter 发布到 Maven Central 等。【deploy发布到远程仓库(maven私服)】


❤觉得有用的可以留个关注ya~~❤

相关文章:

  • 语法: result=fmod(val1, val2)
  • python3最新版下载及python 3.13.1安装教程(附安装包)
  • DeepSeek自学手册:《从理论(模型训练)到实践(模型应用)》|73页|附PPT下载方法
  • δ函数相关的定义和性质
  • 免费下载 | 2025低空经济产业发展报告
  • 什么是嵌入式处理器
  • 玄机-第四章 windows实战-wordpress的测试报告
  • Windows系统提权
  • 《Git:基本命令使用》
  • 【python】12. File
  • QT多线程实战经验
  • 深入C++:operator new与operator delete重载探秘
  • 常用数据库远程连接工具全解析:从入门到高效管理
  • MySQL Router被HTTP流量击穿
  • 读《浪潮之巅》:探寻科技产业的兴衰密码
  • 为AI聊天工具添加一个知识系统 之147 设计重审 之12 聚合AI
  • Vue.js 模板语法全解析:从基础到实战应用
  • 机场上云-无人机状态上报流程
  • 【标准协议】RTCA标准协议解读
  • linux安装nginx详细教程
  • 多地景区发公告称售票达接待峰值,有景区暂停网络和线下售票
  • 侧记|“五五购物节”启动!最大力度补贴,买买买 “666”
  • 中央网信办:重点整治违规AI产品、利用AI制作发布谣言等突出问题
  • 习近平主持召开部分省区市“十五五”时期经济社会发展座谈会
  • 比黄油年糕热量还高,这个火爆全网的甜品劝你慎吃
  • 打造全域消费场景,上海大世界百个演艺娱乐新物种待孵化