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

自定义SpringBoot 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/  #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();
}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模块

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

<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>custom-hello-spring-boot-starter</artifactId><version>1.0.0</version><packaging>pom</packaging><dependencies><!-- 引入jdk模块 --><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

备注:spring.factories文件也可以放到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</artifactId><version>1.0.0</version>
</dependency>

step2. 配置属性(可选)

若不配置,则使用HelloProperties中设置的默认值:

custom.hello.message=Hello from Config!

step3.使用Bean

可直接使用@Autowired引入目标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 实现灵活配置

相关文章:

  • 当K8S容器没有bash时高阶排查手段
  • Github上如何准确地搜索开源项目
  • (二)毛子整洁架构(CQRS/Dapper/DomianEvent Handler)
  • 8.软考高项(信息系统项目管理师)-沟通管理
  • 作为主动唤醒的节点,ECU上电如何请求通讯
  • String、StringBuilder、StringBuffer的区别
  • 翻转二叉树(简单)
  • 使用原生javascript手动实现一个可选链运算符
  • 牛客——暴力、技巧、字符与数组的使用(强强联合、字符数量)
  • 【工具】解析URL获取实际图片地址下载原始FFHQ图像
  • C++:实现线程池
  • VMware中虚拟机和主机的SSH远程连接
  • langchain使用推理模型如DeepSeek,删除回答中的推理过程<think></think>
  • 数据库实验10 函数存储
  • vitepress 复杂环境引入 mermaid
  • Python技巧:TX串口输入十六进制字符串,并获取输出,RX获取输出;循环1000次,通过分析RX输出,计算丢包率。
  • 使用docker配置Mysql
  • 深度学习:图神经网络GNN、GCN及其在推荐系统的应用
  • 人工智能 计算智能领域中分布估计算法的核心思想
  • 影刀RPA中使用AI模型
  • 詹丹|高考语文阅读题设计和答案拟制的一些缺憾
  • 李云泽:再批复600亿元,进一步扩大保险资金长期投资试点范围
  • 一揽子十条货币政策措施出炉:降准降息,设立五千亿服务消费与养老再贷款
  • “穿越看洪武”,明太祖及其皇后像台北故宫博物院南院展出
  • 铁路上海站迎五一返程客流最高峰,今日预计到达75.9万人次
  • 出现这几个症状,说明你真的老了