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

【Spring Boot】自定义starter

为什么要自定义Starter

Spring Boot Starter 是一种简化依赖管理和自动配置的机制。下面详细介绍如何创建自定义 Starter。

Starter 的基本概念

命名规范

官方 Starter: spring-boot-starter-{name}
自定义 Starter: {name}-spring-boot-starter

核心组件

autoconfigure 模块: 包含自动配置逻辑

starter 模块: 只包含依赖管理

创建自定义 Starter

项目结构

my-starter/
├── my-spring-boot-autoconfigure/
│   ├── src/main/java/
│   │   └── com/example/mystarter/
│   └── pom.xml
└── my-spring-boot-starter/├── src/main/resources/│   └── META-INF/│       └── spring.factories└── pom.xml

自动配置模块

<!-- my-spring-boot-autoconfigure/pom.xml -->
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
</dependencies>

配置属性类

@ConfigurationProperties(prefix = "my.service")
public class MyServiceProperties {private String prefix = "Hello";private String suffix = "!";// getters and setterspublic String getPrefix() { return prefix; }public void setPrefix(String prefix) { this.prefix = prefix; }public String getSuffix() { return suffix; }public void setSuffix(String suffix) { this.suffix = suffix; }
}

服务类

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 wrap(String message) {return prefix + " " + message + " " + suffix;}
}

自动配置类

@Configuration
@ConditionalOnClass(MyService.class)
@EnableConfigurationProperties(MyServiceProperties.class)
public class MyServiceAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic MyService myService(MyServiceProperties properties) {return new MyService(properties.getPrefix(), properties.getSuffix());}@Bean@ConditionalOnProperty(name = "my.service.enabled", havingValue = "true")@ConditionalOnMissingBeanpublic MyServiceController myServiceController(MyService myService) {return new MyServiceController(myService);}
}

注册自动配置

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.mystarter.MyServiceAutoConfiguration

需要注意,spring boot 3.x 取消了spring.factories, 替代方案为在路径 src/main/resources/META-INF/spring 创建一个文件org.springframework.boot.autoconfigure.AutoConfiguration.imports

把需要自动装配一行一行放入

com.example.mystarter.MyServiceAutoConfiguration
http://www.dtcms.com/a/457568.html

相关文章:

  • 微网站建设网络温州大军建设有限公司网站
  • 残差特征蒸馏网络(RFDN)探索札记:轻量化图像超分的突破
  • 一般做网站什么价格可以做公众号的网站
  • 优选算法---字符串
  • 任丘网站建设资料查询网站怎么做
  • 华为OD机试C卷 - 流量波峰 - 暴力搜索 - (Java C++ JavaScript Python)
  • 使用CSS3动画属性实现斜线动画 -- 弧线动画 -- 波纹动画 -- 点绕圆旋转动画 -- 浮动动画
  • 打工人日报#20251008
  • 手机网站触摸版萧山中兴建设有限公司网站
  • Python游戏开发入门:从零开始制作贪吃蛇小游戏
  • kanass入门到实战(11) - Kanass如何有效集成sward文档
  • 尚硅谷SpringBoot3零基础教程,课程介绍,笔记01
  • 51网站统计德州网站建设的公司
  • C++23 高级编程 Professional C++, Sixth Edition(一)
  • Verilog和FPGA的自学笔记3——仿真文件Testbench的编写
  • 记录gitee的使用
  • 动态业务流程的案例管理标准(CMMN)
  • 广东门户网站建设哪个网站有适合小学生做的题
  • .NET周刊【9月第4期 2025-09-28】
  • 一级a做爰片365网站天门建设局官方网站
  • 电子商城网站制作广东网站营销seo费用
  • HarmonyOS应用开发 - 无受限权限保存资源到媒体库
  • 网上书店电子商务网站建设企业网站模板下载psd格式
  • 京东手机项目:手机受欢迎的影响因素分析
  • linux zgrep命令介绍
  • 成都著名网站建设公司php 抓取 wordpress 文字内容
  • 高性能Go协程库ants实战指南(二)
  • [Android] 【最新更新】电子书/小说下载APP 遇见云书V3.2.0
  • golang面经——map模块和sync.Map模块
  • 【Pandas】pandas Index objects DatetimeIndex.dayofyear