手写SpringBoot Starter(一):10分钟带你入门,从此告别重复配置!


系列文章第1篇 | 共5篇

难度:⭐ | 适合人群:SpringBoot初学者


💥 开场:一个程序员的崩溃现场

时间: 某个平平无奇的周三下午
地点: 工位
人物: 我,一个刚入职的小菜鸟

领导: "小王啊,把咱们的Redis配置加到新项目里吧。"
我: "好嘞!"(心想:不就是复制粘贴嘛,简单!)

5分钟后...

java 复制代码
// 第1个项目 - 用户服务
@Configuration
public class RedisConfig {
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName("localhost");
        config.setPort(6379);
        // ...一堆配置
        return new LettuceConnectionFactory(config);
    }
    
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        // ...又是一堆配置
    }
}

1小时后...

我: "领导,Redis配好了!"
领导: "嗯,顺便把订单服务、商品服务、支付服务也都加上Redis吧。"
我: "......" (内心:这要复制多少遍啊???)

又过了1小时...

java 复制代码
// 第2个项目 - 订单服务(复制粘贴)
@Configuration
public class RedisConfig {
    // 一模一样的代码...
}

// 第3个项目 - 商品服务(复制粘贴)
@Configuration
public class RedisConfig {
    // 又是一模一样的代码...
}

// 第4个项目...
// 第5个项目...
// 第100个项目...

我内心: "崩溃!这不是纯纯的体力活吗?难道程序员的价值就是Ctrl+C、Ctrl+V?" 😭


就在我准备摆烂的时候...

老鸟同事路过: "哟,还在复制粘贴呢?用Starter啊!"
我: "Starter?那是啥?" 🤔
老鸟: "你看啊..."

xml 复制代码
<!-- 只需要加这一行依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
yaml 复制代码
# 只需要在配置文件写这几行
spring:
  redis:
    host: localhost
    port: 6379
java 复制代码
// 然后直接用,啥配置类都不用写!
@Autowired
private RedisTemplate<String, Object> redisTemplate;

我: "卧槽!这么简单???" 😱
老鸟: "这就是SpringBoot Starter的魅力!"


🤔 第一问:Starter到底是个啥?

Q1:说人话,Starter是啥?

官方定义:

SpringBoot Starter是一种依赖描述符,它将常用的依赖和配置进行统一封装,遵循"约定大于配置"的理念。

看完: "说了等于没说..." 🙄

人话版本:

想象你去麦当劳点餐:

  • 不用Starter: "我要一个汉堡、一份薯条、一杯可乐、一份鸡块、两包番茄酱、三包糖..."(报菜名.jpg)
  • 用Starter: "给我来个套餐A!" ✅

Starter就是SpringBoot的"套餐"!

把一堆相关的依赖、配置、代码打包成一个"套餐",你只需要:

  1. 引入一个依赖(点套餐)
  2. 写几行配置(加不加冰)
  3. 直接用(开吃)

Q2:为啥叫Starter不叫Plugin?

我的疑问: "为啥不叫spring-boot-plugin-redis,偏偏叫starter?"

理解的过程:

第一层理解: Starter = 启动器

  • 它能"启动"某个功能
  • 比如spring-boot-starter-web启动Web功能

第二层理解: Starter = 新手包

  • 就像游戏里的"新手礼包"
  • 包含了你需要的一切,开箱即用

第三层理解: Starter = 一键配置

  • 不需要你手动配置一大堆东西
  • SpringBoot自动帮你配好

恍然大悟: "原来Starter的意思是'开始',引入它就能开始用某个功能,妙啊!" ✨


Q3:Starter和普通Jar包有啥区别?

场景对比:

场景A:使用普通Jar包(MyBatis为例)

xml 复制代码
<!-- 需要引入一堆依赖 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.7</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>
<!-- 还有一堆...头都大了 -->
java 复制代码
// 需要写一堆配置类
@Configuration
public class MyBatisConfig {
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        // 一堆配置...
        return factoryBean.getObject();
    }
    
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        // 又是一堆配置...
    }
}

我的状态: 😵 "写了半天配置,还没开始写业务代码..."


场景B:使用Starter(MyBatis Starter)

xml 复制代码
<!-- 只需要一个依赖 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>
yaml 复制代码
# 只需要几行配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.entity
java 复制代码
// 直接开始写业务代码
@Mapper
public interface UserMapper {
    User findById(Long id);
}

我的状态: 😎 "这才叫生产力工具!"


总结对比:

维度 普通Jar包 Starter
依赖管理 需要手动引入多个依赖 一个依赖搞定
版本兼容 需要自己找兼容版本 版本已经测试好了
配置代码 需要写大量配置类 自动配置,零代码
上手难度 需要看文档研究 开箱即用
使用体验 😫 痛苦 😊 舒适

🎁 第二问:Starter有哪些好处?

好处1:告别配置地狱 🔥

没有Starter的时代:

java 复制代码
// Redis配置类 - 100行
@Configuration
public class RedisConfig { ... }

// MyBatis配置类 - 150行
@Configuration  
public class MyBatisConfig { ... }

// RabbitMQ配置类 - 80行
@Configuration
public class RabbitConfig { ... }

// Elasticsearch配置类 - 120行
@Configuration
public class ElasticsearchConfig { ... }

// ...还有一堆配置类

有了Starter:

yaml 复制代码
# 一个配置文件搞定
spring:
  redis:
    host: localhost
  datasource:
    url: jdbc:mysql://localhost:3306/db
  rabbitmq:
    host: localhost
  elasticsearch:
    uris: http://localhost:9200

感受: "从地狱到天堂,就差一个Starter!" 😇


好处2:统一依赖管理 📦

翻车现场:

xml 复制代码
<!-- 小王的项目 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version> <!-- 注意版本 -->
</dependency>

<!-- 老李的项目 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version> <!-- 版本不一样 -->
</dependency>

结果:

  • 小王的项目用了旧API,能跑
  • 老李的项目用了新API,也能跑
  • 两人代码合并:💥 炸了!

用了Starter:

xml 复制代码
<!-- 所有人都用这个 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

Starter内部自动管理:

  • mybatis: 3.5.9
  • mybatis-spring: 2.0.7
  • spring-boot: 2.6.x

结果: 版本统一,天下太平! ✌️


好处3:开箱即用 📦→✨

对比体验:

java 复制代码
// 传统方式:需要理解原理才能用
@Configuration
public class RedisConfig {
    // 需要知道 RedisConnectionFactory 是啥
    // 需要知道 RedisTemplate 怎么配
    // 需要知道序列化器怎么设置
    // ...一堆专业知识
}

// Starter方式:不需要懂原理也能用
@Autowired
private RedisTemplate<String, Object> redisTemplate;

public void test() {
    redisTemplate.opsForValue().set("key", "value"); // 直接用!
}

感悟: "这就是'约定大于配置'的魅力,不需要懂原理也能快速上手!" 💡


📚 第三问:官方提供了哪些Starter?

SpringBoot官方Starter家族

Web开发系列:

xml 复制代码
<!-- Web应用(内嵌Tomcat) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- WebFlux(响应式Web) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<!-- WebSocket -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

数据库系列:

xml 复制代码
<!-- JDBC -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!-- JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- MongoDB -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

消息队列系列:

xml 复制代码
<!-- RabbitMQ -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

<!-- Kafka -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-kafka</artifactId>
</dependency>

安全与监控:

xml 复制代码
<!-- Spring Security -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- Actuator(健康检查) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

测试系列:

xml 复制代码
<!-- 测试 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

看完: "好家伙,这么多Starter!官方想得真周到!" 👍


第三方Starter(非官方但很常用)

xml 复制代码
<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

<!-- Druid数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>

<!-- PageHelper分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
</dependency>

命名规范:一看就懂的潜规则

Q: "为啥有的叫spring-boot-starter-xxx,有的叫xxx-spring-boot-starter?"

A: 这是有讲究的!

规则1:官方Starter命名

markdown 复制代码
格式:spring-boot-starter-{name}
     ↓                      ↓
   官方标识              功能名称

示例:
- spring-boot-starter-web
- spring-boot-starter-data-redis
- spring-boot-starter-security

特点: 官方出品,质量保证!


规则2:第三方Starter命名

markdown 复制代码
格式:{name}-spring-boot-starter
      ↓                       ↓
   功能名称              Starter标识

示例:
- mybatis-spring-boot-starter
- druid-spring-boot-starter
- pagehelper-spring-boot-starter

特点: 社区贡献,百花齐放!


记忆技巧:

  • spring-boot-starter开头 = 官方亲儿子 👶
  • spring-boot-starter结尾 = 第三方干儿子 🤝

反面教材(不推荐):

xml 复制代码
<!-- ❌ 不规范命名 -->
<artifactId>starter-myproject</artifactId>
<artifactId>my-starter</artifactId>
<artifactId>custom-spring-starter</artifactId>

建议: 遵守规范,做个体面人! 🎩


🎮 第四问:来个实战,感受Starter的魅力!

实战场景:给项目加上Redis

需求: 在SpringBoot项目中使用Redis


方式A:不用Starter(传统方式)

步骤1:引入一堆依赖

xml 复制代码
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.7.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.11.1</version>
</dependency>
<!-- 还需要其他依赖... -->

步骤2:写配置类(约50行代码)

java 复制代码
@Configuration
public class RedisConfig {
    
    @Value("${redis.host}")
    private String host;
    
    @Value("${redis.port}")
    private int port;
    
    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(20);
        config.setMinIdle(10);
        config.setTestOnBorrow(true);
        // ...一堆配置
        
        return new JedisPool(config, host, port, 2000);
    }
    
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        // 配置序列化器
        // 配置Key序列化
        // 配置Value序列化
        // ...又是一堆配置
        return template;
    }
}

步骤3:配置文件

properties 复制代码
redis.host=localhost
redis.port=6379
redis.password=
redis.database=0
redis.timeout=2000
redis.pool.max-active=100
redis.pool.max-idle=20
redis.pool.min-idle=10
# ...一堆配置

步骤4:使用

java 复制代码
@Service
public class UserService {
    @Autowired
    private JedisPool jedisPool;
    
    public void saveUser(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
        }
    }
}

耗时: 30分钟-1小时(还得查文档)
代码量: 约100行
心情: 😫 "累死了,还没开始写业务..."


方式B:使用Starter(推荐方式)

步骤1:引入依赖(1行)

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

步骤2:配置文件(4行)

yaml 复制代码
spring:
  redis:
    host: localhost
    port: 6379

步骤3:直接使用(0行配置代码)

java 复制代码
@Service
public class UserService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    public void saveUser(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }
    
    public String getUser(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}

耗时: 2分钟
代码量: 5行
心情: 😎 "这才是开发的正确打开方式!"


效果对比

对比项 传统方式 Starter方式 提升
依赖管理 需要自己找3+个依赖 1个依赖搞定 效率↑200%
配置代码 约100行Java代码 0行 效率↑100%
配置文件 10+行properties 4行yaml 简洁↑60%
上手时间 30-60分钟 2分钟 速度↑15倍
出错概率 高(配置复杂) 低(自动配置) 稳定性↑

结论: Starter = 生产力工具!不用就亏了! 💰


🎯 知识点总结

本篇你学到了什么?

Starter的本质

  • 它是SpringBoot的"套餐"机制
  • 把依赖+配置+代码打包成一个整体
  • 遵循"约定大于配置"理念

Starter的三大好处

  • 告别配置地狱
  • 统一依赖管理
  • 开箱即用

命名规范

  • 官方:spring-boot-starter-{name}
  • 第三方:{name}-spring-boot-starter

实战体验

  • 对比传统方式和Starter方式
  • 感受到了Starter带来的效率提升

🤔 思考题

留几个问题,下期文章开头会解答哦:

  1. Starter是怎么做到"自动配置"的? 🤔

    • 为啥引入依赖就能用?
    • SpringBoot是怎么知道要配置什么的?
  2. 我能不能自己写一个Starter? 💭

    • 比如公司内部的通用功能
    • 怎么封装成Starter?
  3. Starter的原理是什么? 🔍

    • spring.factories是啥?
    • @EnableAutoConfiguration做了什么?

如果你也好奇这些问题,记得关注下一篇! 👇


📢 下期预告

《手写SpringBoot Starter(二):手把手教你写第一个Starter,原来这么简单!》

下一篇我们将:

  • 从0到1创建一个Starter项目
  • 手写自动配置类
  • 理解spring.factories的作用
  • 打包测试验证

保证让你看完就能自己写出一个Starter! 🎉


💬 互动时间

你在实际开发中用过哪些Starter?
有没有遇到过Starter相关的坑?
想自己封装什么样的Starter?

欢迎在评论区分享你的经验和想法!💭


觉得有帮助?别忘了三连支持: 👍 点赞 | ⭐ 收藏 | 🔄 转发

看完这篇,从此告别重复配置,拥抱高效开发! 🚀


下一篇见! 👋


相关推荐
初见0013 小时前
🌱 SpringBoot自动配置:别装了,我知道你的秘密!🤫
spring boot·后端
lang201509283 小时前
Spring Boot核心功能深度解析
spring boot
计算机毕业设计小帅5 小时前
【2026计算机毕业设计】基于Springboot的校园失物招领小程序
spring boot·小程序·课程设计
7哥♡ۣۖᝰꫛꫀꪝۣℋ6 小时前
Spring Boot
java·spring boot·后端
lang201509286 小时前
Spring Boot开发最佳实践速览
spring boot
哲此一生9846 小时前
创建一个SpringBoot项目(连接数据库)
java·spring boot·后端
摇滚侠6 小时前
Spring Boot 3零基础教程,WEB 开发 HttpMessageConverter @ResponseBody 注解实现内容协商源码分析 笔记33
java·spring boot·笔记
计算机毕业设计小帅7 小时前
【2026计算机毕业设计】基于Springboot的校园电动车短租平台
spring boot·后端·课程设计
superlls7 小时前
(Spring)Spring Boot 中 @Valid 与全局异常处理器的联系详解
java·spring boot·后端