【Spring Boot】自定义starter

news/2025/9/26 12:05:31/文章来源:https://www.cnblogs.com/WilsonPan/p/19111609

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.mzph.cn/news/918225.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

redis-bitMap类型基本命令

redis-bitMap类型基本命令BitMap是reids2.2.0版本中引入的一种新的数据类型。该数据类型本质上就是一个只包含0和1的二进制字符串。 它的所有命令都是对这个字符串二进制位的操作。字符串中的每个字符对应的索引称为of…

PrintNightmare漏洞仍未终结:深入解析PnP配置绕过与防护方案

本文深入分析了PrintNightmare漏洞的最新演变,详细介绍了攻击者如何通过DNS欺骗绕过Point and Print安全策略,并评估了UNC路径加固、RPC over SMB等多种防护方案的有效性。文章通过实际测试展示了防护措施的局限性,…

Go 1.26 内置函数 new 新特性

目前golang 1.26的各种新特性还在开发中,不过其中一个在开发完成之前就已经被官方拿到台面上进行宣传了——内置函数new功能扩展。 每个新特性其实都有它的背景故事,没有需求的驱动也就不会有新特性的诞生。所以在介…

基于SpringBoot及PostgreSQL的国家减肥食谱管理项目(上):区域与省份安装搭建

基于SpringBoot及PostgreSQL的国家减肥食谱管理项目(上):区域与省份安装搭建pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-f…

基于BP神经网络的激光焊接数据预测

基于BP神经网络的激光焊接数据预测的系统化方法,结合核心原理、实现步骤及工程优化策略,适用于焊接质量控制和工艺参数优化: 一、BP神经网络原理与激光焊接预测适配性核心机制 BP神经网络通过误差反向传播调整权重,…

重要公式 - Emi

重要的三角函数公式 \[sin\alpha cos\beta=\frac{sin(\alpha+\beta)+sin(\alpha-\beta)}{2} \]\[cos\alpha cos\beta=\frac{cos(\alpha+\beta)+cos(\alpha-\beta)}{2} \]\[sin\alpha sin\beta=-\frac{cos(\alpha+\beta…

Pandawiki:企业知识管理的全能管家

Pandawiki:企业知识管理的全能管家在当今这个信息爆炸的时代,每个企业都面临着一个共同的挑战:如何有效地管理和利用内部积累的海量知识资产?从产品文档到技术规范,从客户问答到项目经验,这些宝贵的信息往往散落…

珠宝网站建设做网站项目流程图模板

Spring的开发要点总结 文章目录 【JavaEE】Spring的开发要点总结&#xff08;1&#xff09;1. DI 和 DL1.1 DI 依赖注入1.2 DL 依赖查询1.3 DI 与 DL的区别1.4 IoC 与 DI/DL 的区别 2. Spring项目的创建2.1 创建Maven项目2.2 设置国内源2.2.1 勾选2.2.2 删除本地jar包2.2.3 re…

apt 还是 uv

一句话结论装系统级软件(C/CUDA、驱动、编译链) → 用 apt 纯 Python 项目/虚拟环境/依赖锁文件 → 用 uv;它比 apt 里的 python3-xxx 新、快、隔离,但不能装非 Python 组件下面给你展开对比,按“能干什么、不能干…

软件构造中的数据处理(sql) 6章

JDBC编程步骤 1.加载数据库驱动 2.建立数据库连接 3.创建Statement/PreparedStatement 4.执行SQL语句 5.处理查询结果(ResultSet) 6.关闭资源(Connection/Statement/ResultSet)

鹿鼎记豪侠传:Rust 重塑 iOS 江湖(下) - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

US$39 CAS Mileage Reset Authorization for CGDI Prog BMW MSV80 CAS1 CAS2 CAS3 CAS3+ via OBD

CAS Mileage Reset Authorization for CGDI Prog BMW MSV80 CAS1 CAS2 CAS3 CAS3+ via OBDWith this authorization, you can change mileage on BMW CAS1 CAS2 CAS3 CAS3+ CAS3++ via OBD.No need shipping. Please p…

是普通网站地图好还是rss地图好一点九江 网站建设

为增强安全性&#xff0c;平台可安装ssl证书。对于平台不同的组成部分需要采用不同的方式&#xff0c;使用不同的证书格式&#xff1a; 一、前端 前端采用nginx部署&#xff0c;安装证书步骤如下&#xff08;linux window版一样&#xff09;&#xff1a; 1、conf目录下增加cert…

树的重心(邻接表)

输入样例:9 1 2 1 7 1 4 2 8 2 5 4 3 3 9 4 6期望输出:4代码实现:#include<bits/stdc++.h> using namespace std;const int N =1e5+10 , M=2*N;int n,m; int h[N],e[M],ne[M],idx; bool vis[N]; int ans=N ;v…

语音芯片怎样接? 语音芯片有哪些常见接口类型?

目录: 语音芯片怎样接? 语音芯片有哪些常见接口类型? UART接口如何实现数据传输? UART与I2C接口有何不同? UART通讯的常见故障有哪些? UART通信中时钟同步的原理: 语音芯片怎样接? 语音芯片的连接方式取决于其…

详细介绍:2025华为杯A题B题C题D题E题F题选题建议思路数学建模研研究生数学建模思路代码文章成品

详细介绍:2025华为杯A题B题C题D题E题F题选题建议思路数学建模研研究生数学建模思路代码文章成品pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !i…

上饶市住房和城乡建设局网站电商培训视频教程

一、分类任务 构建分类网络模型 必须继承nn.Module且在其构造函数中需调用nn.Module的构造函数无需写反向传播函数&#xff0c;nn.Module能够利用autograd自动实现反向传播Module中的可学习参数可以通过named_parameters()返回迭代器 from torch import nn import torch.nn.f…

Gitee vs. GitLab:中国开发者为何选择本土代码托管平台?

Gitee vs. GitLab:中国开发者为何选择本土代码托管平台? 在数字化转型和信创产业蓬勃发展的背景下,中国开发者正面临一个关键选择:是继续依赖国际化的GitLab,还是拥抱本土化的Gitee?作为国内最大的代码托管平台,…

AtCoder Beginner Contest 424

Atcoder 424 A-F题解A - Isosceles 核心代码: signed main() {ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);int a, b, c;cin >> a >> b >> c;if(a == b || b == c || a == c) cout << …

US$39 BAV-Key Adapter for Yanhua Mini ACDP

BAV-Key Adapter for Yanhua Mini ACDPBAV-Key Adapter will be needed for Module 1 BMW CAS1-CAS4+, Modul 9 Land Rover and Module10 for Porsche.Package List:1pc x BAV-Key Adapter for Yanhua Mini ACDP Pictu…