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

自定义一个 Spring Boot Starter -笔记

SpringBoot Starter的介绍参考: Spring Boot Starter简介-笔记-CSDN博客。这里介绍如何自定义一个springBoot Starter。

1. 项目结构

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

custom-spring-boot-starter-demo/
├── custom-hello-jdk/  # jdk模块,包含功能逻辑
├── custom-hello-spring-boot-starter-jdk/  #Starter模块

2. 项目代码

2.1 custom-hello-jdk模块

step1. pom.xml

<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>custom-hello-jdk</artifactId><version>1.0.0</version><dependencies><!-- Spring Boot 自动配置依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency></dependencies>
</project>

step2. 定义功能接口和实现类

package com.example.demo;public interface HelloService {String sayHello();
}//----------------------------------------------------package com.example.demo;import org.springframework.stereotype.Service;@Service
public class DefaultHelloService implements HelloService {private final String message;public DefaultHelloService(String message) {this.message = message;}@Overridepublic String sayHello() {return message;}
}

step3. 配置属性类

package com.example.demo;import org.springframework.boot.context.properties.ConfigurationProperties;@Data
@ConfigurationProperties(prefix = "custom.hello")
public class HelloProperties {private String message = "Hello from Custom Starter!";
}

step4. 自动配置类

package com.example.demo;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {private final HelloProperties helloProperties;public HelloAutoConfiguration(HelloProperties helloProperties) {this.helloProperties = helloProperties;}@Bean@ConditionalOnMissingBean(HelloService.class)public HelloService helloService() {return new DefaultHelloService(helloProperties.getMessage());}
}

2.2 custom-hello-spring-boot-starter-jdk模块

step1.pom.xml添加对custom-hello-jdk的依赖

<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>custom-hello-spring-boot-starter-jdk</artifactId><version>1.0.0</version><packaging>pom</packaging><dependencies><!-- 引入自动配置模块 --><dependency><groupId>com.example</groupId><artifactId>custom-hello-jdk</artifactId><version>1.0.0</version></dependency></dependencies>
</project>

step2.注册自动配置

在 src/main/resources/META-INF/spring.factories 中添加:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.demo.HelloAutoConfiguration

备注:自动配置注册也可以放到custom-hello-jdk模块里,在starter里仅放pom文件,作为依赖聚合,方便用户引入。

step3.构建和发布

  1. 执行 mvn clean install 将 Starter 安装到本地 Maven 仓库
  2. 或通过 mvn deploy 发布到远程仓库

2.3 使用自定义 Starter

背景:在另外一个项目中使用自定义的Starter。

step1. 在另一个 Spring Boot 项目中引入依赖

<dependency><groupId>com.example</groupId><artifactId>custom-hello-spring-boot-starter-jdk</artifactId><version>1.0.0</version>
</dependency>

step2. 配置属性(可选)

custom.hello.message=Hello from Config!

step3.使用Bean

import com.example.demo.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@Autowiredprivate HelloService helloService;@GetMapping("/hello")public String hello() {return helloService.sayHello();}
}

2.4 总结

上述自定义springBoot Starter的完整流程说明:

  1. jdk功能模块:包含实际功能代码、配置属性等
  2. Starter 模块:作为依赖聚合,方便用户引入
  3. 条件化配置:通过 @ConditionalOnMissingBean 避免重复 Bean
  4. 属性绑定:使用 @ConfigurationProperties 实现灵活配置

相关文章:

  • 广州华锐视点邀您参与2025广交会VRAR展【5月10-12日】
  • 大数据产品销售数据分析:基于Python机器学习产品销售数据爬虫可视化分析预测系统设计与实现
  • 20250506格式化NanoPi NEO开发板使用Ubuntu core16.04系统的TF启动卡
  • Spark 的 Shuffle 机制:原理与源码详解
  • 医疗健康软件专利:给生命科学装个 “智能防盗门“
  • vue项目中渲染markdown并处理报错
  • 电池热管理CFD解决方案,为新能源汽车筑安全防线
  • 汽车紧固件防腐3.0时代:敦普水性漆用无铬锌铝涂层定义「零氢脆」标准
  • 人工智能与生命科学的深度融合:破解生物医学难题,引领未来科技革命
  • Qt—鼠标移动事件的趣味小程序:会移动的按钮
  • 2025最新vmware-17虚拟机安装教程(保姆级,图文讲解,带安装包)
  • MySQL基础关键_009_DDL 和 DML(二)
  • 多线程2-多线程编程
  • 【Fifty Project - D23】
  • 从入门到登峰-嵌入式Tracker定位算法全景之旅 Part 7 |TinyML 定位:深度模型在 MCU 上的部署
  • 扩增子分析|微生物生态网络稳定性评估之鲁棒性(Robustness)和易损性(Vulnerability)在R中实现
  • Jetpack Compose 自定义 Slider 完全指南
  • Javase 基础加强 —— 05 Map集合
  • 图形化编程重塑 IoT 边缘开发:技术革新与生态竞合新范式
  • WebRTC ICE 服务器搭建
  • 上海:5月8日起5年以上首套个人住房公积金贷款利率下调至2.6%
  • 央行宣布优化两项支持资本市场的货币政策工具
  • 象屿集团:对去化压力大、市场有效需求不足区域坚决暂停投资,打造多元上市路径
  • 马上评|比起奇葩论文,更可怕的是“水刊”灰产
  • 严正交涉!我驻日使馆:如日方采取新的挑衅举动,中方必坚决反制
  • 爱彼迎:一季度总收入约23亿美元,将拓展住宿以外的新领域