在 Spring Boot 中自定义一个 Starter(例如 xxx-spring-boot-starter
),可以让其他项目通过依赖引入你的 Starter 来自动配置 Bean 和相关组件。以下是详细步骤:
1. 自定义 Starter 的核心步骤
1.1 项目结构
创建一个 Maven 或 Gradle 项目,通常包含以下模块:
- 核心功能模块:包含业务逻辑和需要自动配置的 Bean。
- 自动配置模块:定义自动配置类、条件注解等。
- Starter 模块(可选):聚合核心功能模块和自动配置模块,供其他项目直接依赖。
my-custom-starter/
├── my-custom-spring-boot-autoconfigure/ # 自动配置模块
│ ├── src/main/java
│ │ └── com/example/autoconfigure/
│ │ ├── MyServiceAutoConfiguration.java # 自动配置类
│ │ └── MyServiceProperties.java # 配置属性类
│ └── pom.xml
└── my-custom-spring-boot-starter/ # Starter 模块(聚合依赖)
└── pom.xml
1.2 编写自动配置类
在 my-custom-spring-boot-autoconfigure
模块中,定义自动配置类:
@Configuration
@EnableConfigurationProperties(MyServiceProperties.class) // 启用配置属性
@ConditionalOnClass(MyService.class) // 当类路径中存在 MyService 时生效
public class MyServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean // 当容器中没有 MyService Bean 时创建
public MyService myService(MyServiceProperties properties) {
return new MyService(properties.getMessage());
}
}
1.3 定义配置属性类
通过 @ConfigurationProperties
绑定配置文件中的属性:
@ConfigurationProperties(prefix = "my.service")
public class MyServiceProperties {
private String message = "default message"; // 默认值
// Getter 和 Setter
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
}
1.4 注册自动配置类
在 src/main/resources/META-INF/
下创建 spring.factories
文件,注册自动配置类:
# META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfigure.MyServiceAutoConfiguration
1.5 创建 Starter 模块(聚合依赖)
在 my-custom-spring-boot-starter
的 pom.xml
中,只需依赖自动配置模块:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-custom-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
2. 其他项目引入 Starter
2.1 添加依赖
其他项目通过 Maven 或 Gradle 引入你的 Starter:
<!-- Maven -->
<dependency>
<groupId>com.example</groupId>
<artifactId>my-custom-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
// Gradle
implementation 'com.example:my-custom-spring-boot-starter:1.0.0'
2.2 使用自动配置的 Bean
直接注入 MyService
:
@RestController
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/message")
public String getMessage() {
return myService.getMessage();
}
}
2.3 自定义配置
在 application.properties
中覆盖默认值:
my.service.message=Hello from custom starter!
3. 高级配置
3.1 条件注解的灵活使用
@ConditionalOnProperty
:根据配置文件中的属性启用配置。@ConditionalOnWebApplication
:仅在 Web 应用中生效。@ConditionalOnMissingClass
:当类路径中不存在某个类时生效。
3.2 添加 Starter 的元数据支持
在 src/main/resources/META-INF/
下创建 spring-configuration-metadata.json
,让 IDE 支持属性提示:
{
"properties": [
{
"name": "my.service.message",
"type": "java.lang.String",
"description": "Custom message for MyService.",
"defaultValue": "default message"
}
]
}
4. 发布 Starter
4.1 本地安装
mvn clean install
4.2 发布到 Maven 仓库
配置 pom.xml
中的 <distributionManagement>
,使用 mvn deploy
发布到 Nexus 或 Maven Central。
总结
通过自定义 Starter,你可以将一组功能封装为可复用的组件,其他项目只需添加依赖即可自动配置 Bean。核心步骤包括:
- 编写自动配置类(使用
@Conditional
注解控制条件)。 - 定义配置属性(
@ConfigurationProperties
)。 - 注册自动配置类(
spring.factories
)。 - 创建 Starter 模块聚合依赖。
这样,用户只需引入 xxx-spring-boot-starter
就能轻松集成你的功能!