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

自定义一个 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 实现灵活配置
http://www.xdnf.cn/news/4054.html

相关文章:

  • 移动应用开发:自定义 View 处理大量数据的性能与交互优化方案
  • Spring AI 与大语言模型工具调用机制详细笔记
  • react-13react中外部css引入以及style内联样式(动态className与动态style)
  • Android开发-工程结构
  • Linux云服务器配置git开发环境
  • day 13 不平衡数据集的处理
  • C++学习知识点汇总
  • git中android studio不想提交文件
  • 【能力比对】K8S数据平台VS数据平台
  • colcon: error: unrecognized arguments: --packages-select报错
  • GD32/STM32 ADC/DMA使用指南
  • QuecPython+腾讯云:快速连接腾讯云l0T平台
  • 巧记英语四级单词 Unit7-中【晓艳老师版】
  • 基于Jaccard算法的用户浏览历史推荐商品系统实战+springboot+vue源码实现
  • 【东枫科技】代理销售 NVIDIA DGX Spark 您的桌上有一台 Grace Blackwell AI 超级计算机。
  • [Survey]Remote Sensing Temporal Vision-Language Models: A Comprehensive Survey
  • C++【继承】
  • 1688平台商品详情接口开发指南(含Python代码示例)
  • 【东枫科技】代理英伟达产品:智能网卡
  • DTU_DTU厂家_5G/4G DTU终端_DTU模块_厦门计讯物联科技有限公司
  • 为什么Transformer推理需要做KV缓存
  • 2025年游戏行业DDoS攻防指南:智能防御体系构建与实战策略
  • 【C++】类和对象(一)
  • 【FreeRTOS-时间管理】
  • 0-1背包问题基础概念
  • 家政平台派单系统设计与实现详解
  • transformer读后感
  • Linux系统编程--基础指令(!!详细讲解+知识拓展)
  • 位运算题目:按位与为零的三元组
  • 代码训练营day56图论岛屿数量与面积