自定义 spring-boot-starter
自定义 Starter 命名规范
- 官方 Starter:
spring-boot-starter-{name},例如:spring-boot-starter-web - 开源自定义 Starter:
{name}-spring-boot-starter,例如:mybatis-spring-boot-starter - 公司内部:
公司名-spring-boot-starter-{name},例如:fly-spring-boot-starter-datasource
自定义 Starter
创建一个 Maven 工程,工程名称为 spring-boot-starter 名称
1、pom 依赖引入
java
<dependencyManagement>
<dependencies>
<!-- spring boot 依赖,统一控制 spring boot 依赖所需版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version> <!-- 引入 spring boot 版本号 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot Starter 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<!-- 如果需要 Actuator 功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<optional>true</optional>
</dependency>
<!-- 自动配置处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 配置元数据注解处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>2、创建配置文件
java
/**
* 配置文件
*
* @author 朔风
* @date 2026-01-16 10:37
*/
@ConfigurationProperties(prefix = MockTestProperties.PREFIX)
public class MockTestProperties {
public static final String PREFIX = "fly.mock";
/**
* 标题
*/
private String title = "Hello Spring Boot Starter";
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}3、编写核心代码
3.1、编写接口代码
java
/**
* 接口
*
* @author 朔风
* @date 2026-01-16 10:32
*/
public interface ITestService {
void handler();
}3.2、默认接口实现
java
/**
* 默认实现
*
* @author 朔风
* @date 2026-01-16 10:32
*/
public class MockTestServiceImpl implements ITestService {
private static final Logger logger = LoggerFactory.getLogger(MockTestServiceImpl.class);
private final MockTestProperties properties;
public MockTestServiceImpl(MockTestProperties properties) {
this.properties = properties;
}
@Override
public void handler() {
logger.info("test {} ===", properties.getTitle());
}
}4、健康检查指示器
java
/**
* 健康检查指示器
*
* @author 朔风
* @date 2026-01-16 11:17
*/
public class FlyTestStarterHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 自定义健康检查逻辑
boolean isHealthy = checkService();
if (isHealthy) {
return Health.up().withDetail("fly-test-starter", "Available").build();
}
return Health.down().withDetail("fly-test-starter", "Unavailable").build();
}
private boolean checkService() {
// 实现检查逻辑
return true;
}
}5、自动注册
java
/**
* 自动配置
*
* @author 朔风
* @date 2026-01-16 10:30
*/
@AutoConfiguration // 标记这是一个自动配置类
@EnableConfigurationProperties(MockTestProperties.class) // 加载自定义文件配置
public class TestStarterAutoConfiguration {
private final MockTestProperties properties;
public TestStarterAutoConfiguration(MockTestProperties properties) {
this.properties = properties;
}
// MockTestServiceImpl 是默认实现,引入方可以自定义实现
// 引入方自定义实现后,该 bean 将不生效
@Bean
@ConditionalOnMissingBean(ITestService.class)
public ITestService mockService() {
return new MockTestServiceImpl(properties);
}
// 健康检查指示器
@Bean
public FlyTestStarterHealthIndicator flyTestStarterHealthIndicator() {
return new FlyTestStarterHealthIndicator();
}
}6、配置 Spring Boot 自动装配
6.1、传统方式(Spring Boot 2.6 之前)
创建 src/main/resources/META-INF/spring.factories,指定 TestStarterAutoConfiguration 类地址
yaml
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.itdachen.boot.TestStarterAutoConfiguration6.2、新方式(Spring Boot 2.7 之后)
创建 src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件,指定 TestStarterAutoConfiguration 类地址
yaml
com.github.itdachen.boot.TestStarterAutoConfiguration7、添加配置元数据
创建 src/main/resources/META-INF/additional-spring-configuration-metadata.json
json
{
"groups": [],
"properties": [
{
"name": "fly.mock.title",
"type": "java.lang.String",
"description": "标题"
}
]
}自定义 starter 的使用
1、引入依赖
yaml
<dependency>
<groupId>com.github.itdachen.boot</groupId>
<artifactId>fly-test-spring-boot-starter</artifactId>
<version>${fly-test.version}</version>
</dependency>2、依赖注入
java
@Autowired
private ITestService testService;3、调用方法
java
testService.handler();
朔风