【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 setters
    public 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
    @ConditionalOnMissingBean
    public MyService myService(MyServiceProperties properties) {
        return new MyService(properties.getPrefix(), properties.getSuffix());
    }
    
    @Bean
    @ConditionalOnProperty(name = "my.service.enabled", havingValue = "true")
    @ConditionalOnMissingBean
    public 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
相关推荐
岳麓丹枫0012 小时前
PostgreSQL 中 pg_wal 目录里的 .ready .done .history 文件的生命周期
数据库·postgresql
Coder_Boy_2 小时前
技术发展的核心规律是「加法打底,减法优化,重构平衡」
人工智能·spring boot·spring·重构
寻星探路6 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
陌上丨9 小时前
Redis的Key和Value的设计原则有哪些?
数据库·redis·缓存
曹牧9 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
AI_56789 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
ccecw9 小时前
Mysql ONLY_FULL_GROUP_BY模式详解、group by非查询字段报错
数据库·mysql
JH30739 小时前
达梦数据库与MySQL的核心差异解析:从特性到实践
数据库·mysql
数据知道9 小时前
PostgreSQL 核心原理:如何利用多核 CPU 加速大数据量扫描(并行查询)
数据库·postgresql