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

如何自定义一个SpringBoot Starter

创建自定义 Spring Boot Starter 涉及封装特定功能供其他项目复用。以下是详细步骤和代码示例:

核心步骤

  1. 项目结构

    创建 Maven 项目(两个模块):

    my-starter-parent
    ├── my-spring-boot-autoconfigure  // 自动配置核心
    └── my-spring-boot-starter        // 轻量依赖聚合

1. 创建自动配置模块 (my-spring-boot-autoconfigure)

① POM 依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>3.1.0</version><optional>true</optional></dependency>
</dependencies>
② 创建配置属性类
@ConfigurationProperties(prefix = "my.service")
public class MyServiceProperties {private String prefix = "Hello";private String suffix = "!";// 省略 getter/setter
}
③ 编写业务服务类
public class MyService {private final String prefix;private final String suffix;public MyService(String prefix, String suffix) {this.prefix = prefix;this.suffix = suffix;}public String greet(String name) {return prefix + " " + name + suffix;}
}
④ 自动配置类(核心)
@Configuration
@EnableConfigurationProperties(MyServiceProperties.class)
@ConditionalOnClass(MyService.class) // 类路径存在时生效
@ConditionalOnProperty(prefix = "my.service", name = "enabled", matchIfMissing = true)
public class MyServiceAutoConfiguration {@Bean@ConditionalOnMissingBean // 用户未定义时初始化public MyService myService(MyServiceProperties properties) {return new MyService(properties.getPrefix(), properties.getSuffix());}
}
⑤ 注册自动配置

src/main/resources/META-INF/spring/创建文件:

文件路径: org.springframework.boot.autoconfigure.AutoConfiguration.imports

com.example.autoconfigure.MyServiceAutoConfiguration

2. 创建 Starter 模块 (my-spring-boot-starter)

POM 配置
<dependencies><dependency><groupId>com.example</groupId><artifactId>my-spring-boot-autoconfigure</artifactId><version>1.0.0</version></dependency>
</dependencies>

📌 ​关键点​:Starter 本身无代码,仅聚合依赖


3. 安装到本地仓库

mvn clean install

4. 在其他项目中使用

添加依赖
<dependency><groupId>com.example</groupId><artifactId>my-spring-boot-starter</artifactId><version>1.0.0</version>
</dependency>
配置参数 (application.yml)
my:service:prefix: "Welcome"suffix: "!!!"# enabled: true # 默认启用
代码调用
@RestController
public class DemoController {@Autowiredprivate MyService myService;@GetMapping("/greet")public String greet(String name) {return myService.greet(name);}
}

高级配置技巧

  1. 条件化Bean

    使用 Spring Boot 的条件注解控制Bean创建:

    @Bean
    @ConditionalOnWebApplication // 仅Web环境生效
    @ConditionalOnMissingBean
    public MyWebService myWebService() {...}
  2. 自定义指标监控

    集成 Micrometer 暴露指标:

    @Bean
    public MyServiceMetrics myServiceMetrics(MyService service) {return new MyServiceMetrics(service);
    }
  3. Starter 元数据

    autoconfigure模块的 META-INF/spring-configuration-metadata.json中添加配置提示:

    {"properties": [{"name": "my.service.prefix","type": "java.lang.String","defaultValue": "Hello","description": "Custom greeting prefix."}]
    }

调试技巧

  1. 启用 debug 模式查看自动配置报告:

    debug: true
  2. 检查 Condition Evaluation Report 中的 Positive matchesNegative matches


通过以上步骤,您已创建了一个可复用、可配置的 Spring Boot Starter。关键点在于解耦自动配置与依赖管理,并遵循 Spring Boot 的约定大于配置原则。实际开发中可结合具体需求扩展错误处理、健康检查等企业级特性。

http://www.dtcms.com/a/343551.html

相关文章:

  • Document Solutions .NET Bundle 8.2.0
  • C++ 入门核心知识
  • 【时时三省】汽车安全 专栏简介
  • strspn函数详解
  • TorchInductor - Introduction
  • 50 C++ STL模板库-算法库 algorithm
  • 使用C++17标准 手写一个vector
  • Python核心技术开发指南(001)——Python简介
  • 基于单片机教室照明灯控制系统
  • 数据结构:生成 (Generating) 一棵 AVL 树
  • 域名污染怎么清洗?域名污染如何处理?
  • 8.21作业
  • 【运维进阶】if 条件语句的知识与实践
  • AI设计师-标小智旗下AI在线设计平台
  • 洛谷 P4942 小凯的数字-普及-
  • Hybrid laser 是什么?
  • BFS算法C++实现(邻接表存储)
  • 最爱--中岛美雪
  • 8 月 20 日科技新动态:多领域创新成果涌现
  • 【typenum】 19 类型相同检查(type_operators.rs片段)
  • Esp32基础(⑩超声波测距模块)
  • Pycharm SSH连接
  • Wireshark数据包波形绘制异常
  • [RestGPT] docs | RestBench评估 | 配置与环境
  • 【51单片机】【protues仿真】基于51单片机16键电子琴系统
  • 【GPT入门】第51课 Conda环境迁移教程:将xxzh环境从默认路径迁移到指定目录
  • OpenAI 开源模型 gpt-oss 是在合成数据上训练的吗?一些合理推测
  • Mysql事务特性
  • python实现根据接口返回数据生成报告和图表
  • (第二十期下)超链接的更多分类