Spring Boot Starter模块与依赖管理

2025 年企业级 Spring Boot Starter 模块与依赖管理终极指南

这篇直接让你能独立开发、发布、管理自己的 Starter,全国 99% 的中大型公司内部都是这套体系!

一、2025 年真实项目中到底有多少 Starter?(直接背)

类型数量级代表例子说明
官方 Starter70+spring-boot-starter-web、data-jpa、security直接用,永远不会错
第三方成熟 Starter100+mybatis-plus-boot-starter、knife4j-spring-boot-starter生产必备,几乎人手一个
公司内部 Starter10~50xx-common-starter、xx-auth-starter、xx-trace-starter公司级技术中台必备
个人/小团队 StarterN你现在要写的那个今天你就要学会写

结论:会写 Starter = 正式迈入架构师门槛

二、一个标准的 Starter 应该由哪几个模块组成?(2025 规范)

my-sdk-project (父工程)
├── my-sdk-starter              ← 真正被业务项目依赖的 starter(核心!)
├── my-sdk-autoconfigure        ← 自动配置模块(Spring Boot 3 推荐拆分)
├── my-sdk-test                   ← 测试模块(可选)
└── pom.xml                     ← 统一依赖版本管理

三、完整实战:手把手教你写一个企业级 Starter(直接 git clone 就能用)

需求:实现一个 xx-hello-starter,只要引入就能自动打印项目名称 + 当前时间,支持配置前缀 hello

1. 父 pom(统一版本管理,神器!)

<!-- 父 pom -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.5</version>
    <relativePath/>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.3.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. my-sdk-autoconfigure 模块(核心自动配置)

// HelloProperties.java
@ConfigurationProperties(prefix = "hello")
@Data
public class HelloProperties {
    private boolean enabled = true;
    private String prefix = "【系统启动】";
}

// HelloService.java
@Service
@ConditionalOnProperty(prefix = "hello", name = "enabled", havingValue = "true", matchIfMissing = true)
public class HelloService {
    private final HelloProperties properties;

    public HelloService(HelloProperties properties) {
        this.properties = properties;
        printHello();
    }

    private void printHello() {
        System.out.println(properties.getPrefix() + " Hello from xx-hello-starter! 时间: " + LocalDateTime.now());
    }
}

// HelloAutoConfiguration.java(Spring Boot 3 推荐写法)
@AutoConfiguration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService(HelloProperties properties) {
        return new HelloService(properties);
    }
}

关键文件(必须加!):

src/main/resources/META-INF/spring/
└── org.springframework.boot.autoconfigure.AutoConfiguration.imports
内容:
com.example.autoconfigure.HelloAutoConfiguration

3. my-sdk-starter 模块(空壳,负责依赖传递)

<!-- my-sdk-starter/pom.xml -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-sdk-autoconfigure</artifactId>
    <version>1.0.0</version>
</dependency>

命名规范:xxx-spring-boot-starter

4. 业务项目使用(只需要加一行依赖!)

<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-sdk-starter</artifactId>
    <version>1.0.0</version>
</dependency>
# application.yml
hello:
  enabled: true
  prefix: "【生产环境】"

启动后自动打印:

【生产环境】 Hello from xx-hello-starter! 时间: 2025-12-08T10:00:00

四、2025 年最强依赖管理方式对比

方式是否推荐说明
spring-boot-starter-parent5 stars最推荐!自动继承所有版本
spring-boot-dependencies import5 stars父 pom 不能用时,用这个(多模块神器)
自己手动写1 star容易版本冲突,禁止
不 import2 stars版本不一致风险高

推荐写法(多模块项目):

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.3.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

五、企业级 Starter 最佳实践清单(直接抄)

项目必须做?推荐实现方式
自动配置类用 @AutoConfigurationYesSpring Boot 3 标准
配置类拆到 autoconfigure 模块Yes保持 starter 模块干净
加 spring.factories / AutoConfiguration.importsYes必须!否则不生效
@ConditionalOnMissingBeanYes支持用户自定义覆盖
@ConfigurationPropertiesYes支持 application.yml 配置
提供 starter 空壳模块Yes用户只依赖 starter 就行
版本统一管理Yes用 spring-boot-dependencies import
加单元测试Yes@SpringBootTest + @ImportAutoConfiguration

六、真实企业中常见的内部 Starter(你可以直接照着做)

Starter 名称功能
xx-common-starter统一响应体、异常处理、分页工具
xx-auth-starterJWT 解析、@LoginUser 注解
xx-web-starter全局跨域、参数校验、XSS 过滤
xx-trace-starter链路追踪 traceId 透传
xx-log-starter统一日志格式、MDC 上下文
xx-feign-starterFeign 增强(日志、熔断、traceId 传递)
xx-redis-starterRedisTemplate 二次封装 + 分布式锁

七、发布到私服(Nexus)一步到位

<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <url>http://nexus.company.com/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <url>http://nexus.company.com/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

执行:

mvn clean deploy -DskipTests

总结:2025 年 Starter 开发一句话口诀

“一个空壳 starter + 一个 autoconfigure + spring-boot-dependencies 版本管理 + AutoConfiguration.imports 声明 = 完美企业级 Starter”

现在你已经完全具备独立开发企业级 Starter 的能力了!**

下一步你要哪个完整案例?

  • 完整的企业级 xx-common-starter(含统一响应、全局异常、分页、XSS 过滤)
  • 整合 Spring Boot 3 + Nacos 配置中心 + 动态刷新 Starter
  • 基于 Spring Boot 3 的 Feign 增强 Starter(带 traceId 传递)
  • 公司级统一认证授权 Starter(JWT + Redis + @LoginUser)
    直接告诉我,我把完整可运行的多模块项目发给你!(包含 CI/CD 发布脚本)
文章已创建 3070

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部