如何自定义一个SpringBoot Starter(步骤和代码示例)
作者:悟能不能悟
创建自定义Spring Boot Starter涉及封装特定功能供其他项目复用,下面给大家详细介绍如何自定义一个SpringBoot Starter,感兴趣的朋友跟随小编一起看看吧
创建自定义 Spring Boot Starter 涉及封装特定功能供其他项目复用。以下是详细步骤和代码示例:
核心步骤
项目结构
创建 Maven 项目(两个模块):
my-starter-parent ├── my-spring-boot-autoconfigure // 自动配置核心 └── my-spring-boot-starter // 轻量依赖聚合
1. 创建自动配置模块 (my-spring-boot-autoconfigure)
① POM 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>3.1.0</version>
<optional>true</optional>
</dependency>
</dependencies>② 创建配置属性类
@ConfigurationProperties(prefix = "my.service")
public class MyServiceProperties {
private String prefix = "Hello";
private String suffix = "!";
// 省略 getter/setter
}③ 编写业务服务类
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 greet(String name) {
return prefix + " " + name + suffix;
}
}④ 自动配置类(核心)
@Configuration
@EnableConfigurationProperties(MyServiceProperties.class)
@ConditionalOnClass(MyService.class) // 类路径存在时生效
@ConditionalOnProperty(prefix = "my.service", name = "enabled", matchIfMissing = true)
public class MyServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean // 用户未定义时初始化
public MyService myService(MyServiceProperties properties) {
return new MyService(properties.getPrefix(), properties.getSuffix());
}
}⑤ 注册自动配置
在 src/main/resources/META-INF/spring/创建文件:
文件路径: org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.autoconfigure.MyServiceAutoConfiguration
2. 创建 Starter 模块 (my-spring-boot-starter)
POM 配置
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>📌 关键点:Starter 本身无代码,仅聚合依赖
3. 安装到本地仓库
mvn clean install
4. 在其他项目中使用
添加依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>配置参数 (application.yml)
my:
service:
prefix: "Welcome"
suffix: "!!!"
# enabled: true # 默认启用代码调用
@RestController
public class DemoController {
@Autowired
private MyService myService;
@GetMapping("/greet")
public String greet(String name) {
return myService.greet(name);
}
}高级配置技巧
条件化Bean
使用 Spring Boot 的条件注解控制Bean创建:
@Bean
@ConditionalOnWebApplication // 仅Web环境生效
@ConditionalOnMissingBean
public MyWebService myWebService() {...}自定义指标监控
集成 Micrometer 暴露指标:
@Bean
public MyServiceMetrics myServiceMetrics(MyService service) {
return new MyServiceMetrics(service);
}Starter 元数据
在 autoconfigure模块的 META-INF/spring-configuration-metadata.json中添加配置提示:
{
"properties": [
{
"name": "my.service.prefix",
"type": "java.lang.String",
"defaultValue": "Hello",
"description": "Custom greeting prefix."
}
]
}调试技巧
启用 debug 模式查看自动配置报告:
debug: true
检查 Condition Evaluation Report 中的 Positive matches和 Negative matches
通过以上步骤,您已创建了一个可复用、可配置的 Spring Boot Starter。关键点在于解耦自动配置与依赖管理,并遵循 Spring Boot 的约定大于配置原则。实际开发中可结合具体需求扩展错误处理、健康检查等企业级特性。
到此这篇关于如何自定义一个SpringBoot Starter(步骤和代码示例)的文章就介绍到这了,更多相关自定义SpringBoot Starter内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
